我有一个包含内容($ unzip -l output.jar
)的 jar 文件:
Archive: output.jar
Length Date Time Name
--------- ---------- ----- ----
0 2013-07-08 17:57 META-INF/
120 2013-07-08 17:57 META-INF/MANIFEST.MF
0 2013-07-08 17:43 some/
0 2013-07-08 17:43 some/package/
1247 2013-07-08 17:57 some/package/Main.class
2032 2013-07-08 17:57 some/package/plsql_o12_lexer$DFA22.class
8022 2013-07-08 17:57 some/package/plsql_o12_lexer$DFA23.class
212573 2013-07-08 17:57 some/package/plsql_o12_lexer.class
--------- -------
223994 11 files
内容META-INF/MANIFEST.MF
为:
Manifest-Version: 1.0
Created-By: 1.6.0_27 (Sun Microsystems Inc.)
Main-Class: some.package.Main
但是,运行命令$ echo $CLASSPATH; java -jar output.jar
. 注意 CLASSPATH 变量已设置。
/usr/share/java/antlr3-runtime-3.2.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/antlr/runtime/CharStream
Caused by: java.lang.ClassNotFoundException: org.antlr.runtime.CharStream
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: some.package.Main. Program will exit.
运行该命令$ java -classpath /usr/share/java/antlr3-runtime-3.2.jar -jar output.jar
会产生完全相同的结果。我也尝试过.:/usr/share/java/antlr3-runtime-3.2.jar
作为.:./output.jar:/usr/share/java/antlr3-runtime-3.2.jar
类路径,结果相同。该文件antlr3-runtime-3.2.jar
确实包含类org.antlr.runtime.CharStream
,我已经检查过了。
但是,运行java -verbose -jar output.jar
会给出包含以下行的结果:
[Loaded some.package.Main from file:/home/jan/projects/antlr-plsql/output.jar]
我的Java是:
java version "1.6.0_27"
OpenJDK Runtime Environment (IcedTea6 1.12.5) (6b27-1.12.5-0ubuntu0.12.04.1)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)
为什么 Java 不能按应有的方式工作?