使用-C
dir选项_
在处理以下inputfiles参数时,在执行命令期间临时更改目录 ( cd
dir ) 。jar
如果您执行jar
问题中的命令并列出 temp.jar 的内容,您将看到类似于以下的输出:
$ rm -rf temp.jar
$ jar cfe temp.jar SigarTest.SigarMain SigarTest/bin/ tools.jar sigar.jar mongo-2.7.3.jar
$ jar tf temp.jar
META-INF/
META-INF/MANIFEST.MF
SigarTest/bin/
SigarTest/bin/SigarTest/
SigarTest/bin/SigarTest/SigarMain.class
tools.jar
sigar.jar
mongo-2.7.3.jar
$ java -jar temp.jar
Exception in thread "main" java.lang.NoClassDefFoundError: SigarTest/SigarMain
Caused by: java.lang.ClassNotFoundException: SigarTest.SigarMain
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
请注意,在 temp.jar 中包含 SigarTest/bin 是不正确的。执行 temp.jar 会抛出ClassNotFoundException
since SigarMain
is in package SigarTest.bin.SigarTest
。现在考虑以下jar
使用-C
dir选项的命令:
$ rm -rf temp.jar
$ jar cfe temp.jar SigarTest.SigarMain -C SigarTest/bin/ . tools.jar sigar.jar mongo-2.7.3.jar
$ jar tf temp.jar
META-INF/
META-INF/MANIFEST.MF
SigarTest/
SigarTest/SigarMain.class
tools.jar
sigar.jar
mongo-2.7.3.jar
$ java -jar temp.jar
SigarMain
位于正确的包中并且执行 temp.jar 不会抛出ClassNotFoundException
.