2013-07-10 42 views
2

在我从一个ant文件构建这个项目之后,我收到一个包含我构建的所有类的jar。当我尝试运行这个罐子,我得到以下错误:在运行时找不到jar的类,但用于编译

Exception in thread "main" java.lang.NoClassDefFoundError: javax/media/j3d/SceneGraphObject 

此错误指示一个罐子中,特别是从j3dcore.jar Java3D的,我使用不能被发现。但是,当通过ant build编译到类文件中时,此jar文件位于类路径中。

为什么在运行时不能找到这个类,但它在编译时发现?当运行jar以及在ant版本中更改它时,是否必须在shell中手动更改我的类路径?

如果我添加的罐子使用java -cp j3d/*.jar -jar idv.jar

我的classpath中我得到的错误Error: Could not find or load main class j3d.j3dutils.jar

回答

6

Do I have to manually change my classpath in my shell when running the jar as well as change it in the ant build?

是的,绝对。在编译时创建一个类不会将类嵌入到输出中或类似的东西中。它只是让编译器可用(找出存在的方法等)。

If I add the jars to my classpath using java -cp j3d/*.jar -jar idv.jar

是的,它会 - 因为这是被扩展为:

java -cp j3d/foo.jar j3d/bar.jar ... -jar idv.jar 

这不是很清楚,我-cp是否打算与-jar在所有的工作,给予this documentation

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

一个选项是set the classpath within the manifest of the jar file itself。例如:

Class-Path: j3d/foo.jar j3d/bar.jar 

另一个办法是忽略现在-jar命令行选项,使用:

java -cp j3d/*:idv.jar your.class.name.Here 

注意*而非*.jar,如记录:

As a special convenience, a class path element containing a basename of * is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR (a java program cannot tell the difference between the two invocations).

+0

感谢Jon的一个非常容易阅读和理解的答案。所有工作。 – nook

+1

*“我不清楚-cp是否打算用-jar工作*。事实上,它不起作用。如果使用'--jar','java'命令将*默认*忽略'-cp'和'$ CLASSPATH'。 –

+0

@StephenC:对 - 这就是我所期望的(除了默默的部分,对于'-cp'),但不想太直率。在我看来,如果你同时指定'-cp'和'-jar',那么它确实应该抱怨... –

相关问题