编译源代码:
假定%jdk_bin%为jdk/bin目录。
%jdk_bin%\Javac test.java
%jdk_bin%\javac com\test.java
编译后在命令行窗口中运行:
假定%jdk_bin%为jdk/bin目录。
%jdk_bin%\java Test
显示结果:
====== Test.class ======
file:/C:/project/
file:/C:/project/
file:/C:/project/
file:/C:/project/Test.class
file:/C:/project/Test.class
file:/C:/project/Test.class
file:/C:/project/com/Test.class
file:/C:/project/com/Test.class
file:/C:/project/com/Test.class
====== com/Test.class ======
file:/C:/project/com/
file:/C:/project/com/
file:/C:/project/
file:/C:/project/com/Test.class
file:/C:/project/Test.class
file:/C:/project/com/Test.class
null
file:/C:/project/com/Test.class
null
打包成可执行Jar后在命令行窗口中运行:
假定%jdk_bin%为jdk/bin目录。
%jdk_bin%\java -jar Test.jar
显示结果:
====== Test.class ======
null
null
null
jar:file:/C:/project/test.jar!/Test.class
jar:file:/C:/project/test.jar!/Test.class
null
jar:file:/C:/project/test.jar!/com/Test.class
jar:file:/C:/project/test.jar!/com/Test.class
null
====== com/Test.class ======
jar:file:/C:/project/test.jar!/com/
null
null
jar:file:/C:/project/test.jar!/com/Test.class
jar:file:/C:/project/test.jar!/Test.class
null
null
jar:file:/C:/project/test.jar!/com/Test.class
null
[参考1]打包成windows双击执行的Jar方法:
1. 在 c:\project 目录下新建 META-INF 目录,在 c:\project\META-INF 目录下新建 MANIFEST.MF 文件,手动生成jar文件的“标明清单(manifest)”。其中“Main-Class: Test”是声明双击jar文件后自动运行的类文件,其中这个类文件一定要有main方法,效果等于运行“java -jar test.jar”命令,但不显示DOS运行窗口。MANIFEST.MF 文件内容如下:
# source begin. #
Manifest-Version: 1.0
Sealed: true
Main-Class: Test
Class-Path:
# source end. #
2. 在命令行窗口中键入以下命令,其中%java_home%为java安装目录,cvfM中的M一定要大写,因为要明确要求jar文件使用我们自己创建的MANIFEST.MF文件,如果使用小写m,则 META-INF 目录及 MANIFEST.MF 由jar命令生成。
假定%jdk_bin%为jdk/bin目录。
%jdk_bin%\jar cvfM test.jar *.*
[参考2]目录树:
C:\project
+- com
| +- Test.class
|
+- META-INF
| +- MANIFEST.MF
|
+- Test.class