本文介绍了jar中的类在运行时找不到,但用于编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从ant文件构建此项目后,将收到一个jar,其中包含我构建的所有类.当我尝试运行此jar时,出现以下错误:

After I build this project from an ant file, I recieve a jar that contains all of the classes I built. When I try to run this jar, I get the following error:

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

此错误表明找不到我使用的其中一个jar,特别是java3d中的 j3dcore.jar .但是,当通过ant构建到类文件中进行编译时,此jar位于类路径中.

This error indicates that a one of the jars, specifically the j3dcore.jar from java3d, I am using can not be found. However, this jar is on the classpath when compiling through the ant build into the class files.

为什么在运行时找不到此类,但在编译时却找到呢?运行jar时是否必须在外壳中手动更改我的类路径,以及在ant构建中更改它?

Why can this class not be found at runtime, but it is found at compile time? Do I have to manually change my classpath in my shell when running the jar as well as change it in the ant build?

如果我使用 java -cp j3d/*.jar -jar idv.jar

我收到错误错误:无法找到或加载主类j3d.j3dutils.jar

推荐答案

是的,绝对如此.使类在编译时可用不会将该类嵌入到您的输出或类似内容中.它只是使编译器可以使用它(找出存在的方法,等等).

Yes, absolutely. Making a class available at compile-time doesn't embed the class into your output or anything like that. It just makes it available to the compiler (to find out what methods are present etc).

是的,因为-它将被扩展为:

Yes, it would - because that's being expanded into:

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

在给定:

It's not clear to me whether -cp is meant to work at all with -jar, given this documentation:

一种选择是在jar文件清单中设置类路径本身.例如:

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

另一种方法是暂时忽略 -jar 命令行选项,并使用:

Another would be to ignore the -jar command-line option for now, and use:

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

请注意,记录的是 * 而不是 *.jar :

Note the * rather than *.jar, as documented:

这篇关于jar中的类在运行时找不到,但用于编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 17:36
查看更多