java 在运行时找不到 jar 中的类,但用于编译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17561622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class in jar not found at runtime, but was used to compile
提问by nook
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:
从 ant 文件构建此项目后,我收到一个包含我构建的所有类的 jar。当我尝试运行这个 jar 时,出现以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/media/j3d/SceneGraphObject
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.
此错误表示j3dcore.jar
找不到我正在使用的 jar 包之一,特别是来自 java3d 的包。但是,当通过 ant 构建编译到类文件中时,这个 jar 位于类路径上。
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?
为什么在运行时找不到这个类,而在编译时却找到了?我是否必须在运行 jar 时手动更改我的 shell 中的类路径以及在 ant 构建中更改它?
If I add the jars to my classpath using java -cp j3d/*.jar -jar idv.jar
如果我将罐子添加到我的类路径中 java -cp j3d/*.jar -jar idv.jar
I get the error Error: Could not find or load main class j3d.j3dutils.jar
我收到错误 Error: Could not find or load main class j3d.j3dutils.jar
回答by Jon Skeet
Do I have to manually change my classpath in my shell when running the jar as well as change it in the ant build?
我是否必须在运行 jar 时手动更改我的 shell 中的类路径以及在 ant 构建中更改它?
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).
是的,一点没错。在编译时使类可用不会将该类嵌入到您的输出或类似的东西中。它只是使其可供编译器使用(以找出存在哪些方法等)。
If I add the jars to my classpath using java -cp j3d/*.jar -jar idv.jar
如果我使用 java -cp j3d/*.jar -jar idv.jar 将 jar 添加到我的类路径中
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:
鉴于此文档,我不清楚是否-cp
完全适用于:-jar
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
使用此选项时,JAR 文件是所有用户类的来源,其他用户类路径设置将被忽略。
One option is to set the classpath within the manifest of the jar file itself. For example:
一种选择是在 jar 文件本身的清单中设置类路径。例如:
Class-Path: j3d/foo.jar j3d/bar.jar
Another would be to ignore the -jar
command-line option for now, and use:
另一种方法是暂时忽略-jar
命令行选项,并使用:
java -cp j3d/*:idv.jar your.class.name.Here
Note the *
rather than *.jar
, as documented:
请注意*
而不是*.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).
为方便起见,包含基名 * 的类路径元素被认为等效于指定目录中所有文件的列表,扩展名为 .jar 或 .JAR(Java 程序无法区分这两种调用之间的区别)。