在运行时,Java jar返回java.lang.NoClassDefFoundError

时间:2015-03-07 09:21:14

标签: java jar

唉,我已经搜索了我的问题的解决方案,包括herehere,但无济于事。

所以情况是我有一个名为bin的文件夹,其中包含我的所有类文件,包括ImpulseResponseFunction。在另一个名为lib的文件夹中,我有所有的jar库。我在META-INF文件夹中创建了以下清单文件:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.7.0_51-b00 (Oracle Corporation)
Main-Class: ImpulseResponseFunction

所以我使用:

创建jar
jar cvfm magicCarbon.jar META-INF/MANIFEST.MF bin/*.class -classpath lib

我得到以下输出:

-classpath : no such file or directory
added manifest
adding: bin/ImpulseResponseFunction$1.class(in = 549) (out= 352)(deflated 35%)
adding: bin/ImpulseResponseFunction.class(in = 3523) (out= 2057)(deflated 41%)
adding: bin/menuBarSepUp$1.class(in = 703) (out= 443)(deflated 36%)
adding: bin/menuBarSepUp$2.class(in = 703) (out= 441)(deflated 37%)
adding: bin/menuBarSepUp$3.class(in = 900) (out= 556)(deflated 38%)
adding: bin/menuBarSepUp.class(in = 1683) (out= 942)(deflated 44%)
adding: bin/MyCanvas.class(in = 865) (out= 482)(deflated 44%)
adding: bin/openImage.class(in = 635) (out= 433)(deflated 31%)
adding: bin/PlotTimeSeries.class(in = 1760) (out= 974)(deflated 44%)
adding: bin/WindowSetUp.class(in = 3723) (out= 1952)(deflated 47%)
adding: lib/(in = 0) (out= 0)(stored 0%)
adding: lib/jfreechart-1.0.19-experimental.jar(in = 13791) (out= 11975)(deflated 13%)
adding: lib/jfreechart-1.0.19-src.jar(in = 2009391) (out= 1927691)(deflated 4%)
adding: lib/hamcrest-core-1.3.jar(in = 45024) (out= 39918)(deflated 11%)
adding: lib/jfreechart-1.0.19-swt.jar(in = 79793) (out= 74024)(deflated 7%)
adding: lib/junit-4.11.jar(in = 245039) (out= 215441)(deflated 12%)
adding: lib/swtgraphics2d.jar(in = 17492) (out= 16614)(deflated 5%)
adding: lib/jfreesvg-2.0.jar(in = 50769) (out= 47864)(deflated 5%)
adding: lib/jfreechart-1.0.19.jar(in = 1561720) (out= 1480724)(deflated 5%)
adding: lib/orsonpdf-1.6-eval.jar(in = 72444) (out= 67006)(deflated 7%)
adding: lib/jcommon-1.0.23.jar(in = 330246) (out= 305996)(deflated 7%)
adding: lib/servlet.jar(in = 80054) (out= 70343)(deflated 12%)
adding: lib/orsoncharts-1.4-eval-nofx.jar(in = 445025) (out= 412412)(deflated 7%)
adding: lib/magicCarbon.jar(in = 4681972) (out= 4682157)(deflated 0%)

首先,关于classpath的内容是什么?

然后我跑步时使用:

java -jar magicCarbon.jar

我明白了:

Exception in thread "main" java.lang.NoClassDefFoundError: ImpulseResponseFunction
Caused by: java.lang.ClassNotFoundException: ImpulseResponseFunctio
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:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
Could not find the main class: ImpulseResponseFunction. Program will exit.

为什么找不到ImpulseResponseFunction?

当我在Eclipse中运行时运行时,程序运行良好且花花公子。当我尝试创建和运行jar文件时出现问题。

如果它有助于我运行Ubuntu。

对此的任何帮助将不胜感激。

由于

2 个答案:

答案 0 :(得分:2)

你的班级是默认包(BTW,这不是一个好主意)。所以它必须是jar的根源。 bin目录不是包,因此不能在jar文件中。而不是

jar cvfm magicCarbon.jar META-INF/MANIFEST.MF bin/*.class -classpath lib

使用

cd bin
jar cvfm ../magicCarbon.jar ../META-INF/MANIFEST.MF *.class

此外,库可能不在jar中。他们必须待在外面。 Class-Path manifest entry告诉jar依赖哪个外部库。

答案 1 :(得分:-1)

最后得到了JB Nizet的帮助。谢谢。

解决方案:

1)确保清单文件(META-INF / MANIFEST.MF)正确并使用类路径引用正确的库。在我的情况下,这是:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.7.0_51-b00 (Oracle Corporation)
Class-Path: lib/jcommon-1.0.23.jar lib/jfreechart-1.0.19.jar
Main-Class: ImpulseResponseFunction

类路径需要相对于您要创建的jar文件的路径位置,或者您可以将完整路径放到库中。换句话说,这也有效:

Class-Path: /full/path/name/lib/jarball.jar

2)cd进入主类所在的目录,因为这需要是清单文件的根目录。

3)运行jar命令。对我来说,就像JB Nizet所说:

jar cvfm ../magicCarbon.jar ../META-INF/MANIFEST.MF *.class