NoClassDefFoundError 的可能原因 [英] Possible reasons for NoClassDefFoundError
问题描述
我得到以下 NoClassDefFoundError
,这很奇怪,因为该类已经存在于库 jar 中.
I'm getting the following NoClassDefFoundError
, which is weird because the class is already present in the library jar.
Exception in thread "main" java.lang.NoClassDefFoundError: abc/test/Test.java
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:795)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:144)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:382)
at java.net.URLClassLoader.access$100(URLClassLoader.java:75)
at java.net.URLClassLoader$1.run(URLClassLoader.java:294)
at java.net.URLClassLoader$1.run(URLClassLoader.java:288)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:287)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:327)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:795)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:144)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:382)
at java.net.URLClassLoader.access$100(URLClassLoader.java:75)
at java.net.URLClassLoader$1.run(URLClassLoader.java:294)
at java.net.URLClassLoader$1.run(URLClassLoader.java:288)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:287)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:327)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:432)
Caused by: java.lang.ClassNotFoundException: abc.test.Test
at java.net.URLClassLoader$1.run(URLClassLoader.java:299)
at java.net.URLClassLoader$1.run(URLClassLoader.java:288)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:287)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:327)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 25 more
这看起来似乎无法找到导致异常的同一个类.我还在ant build.xml
中包含了库路径,可以在下面进行验证.
This looks like its not able to find the same class which is causing the exception.
I have also included the library path in ant build.xml
, which can be verified below.
<property name="deps" value="${lib}/abclib/abclib-test.jar"/>
<target name="dist" depends="compile">
<manifest file="${dist}/MANIFEST.MF">
<attribute name="Main-Class" value="xyz.test.TestConfiguration" />
<attribute name="Class-Path" value="${deps}"/>
</manifest>
<jar jarfile="${dist}/abc.jar" basedir="${build}/" manifest="${dist}/MANIFEST.MF" />
</target>
我迷路了,至少有人可以指导我应该看哪里或者我可能做错了什么吗?
I'm lost, can someone at least guide me where should I look or what I might be doing wrong?
此外,有人可以抛出异常"和由"引起的光.我不太明白他们两个有什么关系.
Also, can someone throw light, "Exception" and "Caused by". I'm not quite getting how are they two related.
推荐答案
NoClassDefFoundError
和 ClassNotFoundException
在另一个 SO 条目:
并不意味着 ... 类不在 CLASSPATH 中.事实上,它恰恰相反.这意味着类 ... 被 ClassLoader 找到,但是在尝试加载类时,它在读取类定义时遇到了错误.这通常发生在所讨论的类具有静态块或成员使用类加载器未找到的类时.因此,要找到罪魁祸首,请查看相关类的源代码……并查找使用静态块或静态成员的代码.
does not mean that the ... class is not in the CLASSPATH. Infact its quite the opposite. It means that the class ... was found by the ClassLoader however when trying to load the class, it ran into an error reading the class definition. This typically happens when the class in question has static blocks or members which use a Class that's not found by the ClassLoader. So to find the culprit, view the source of the class in question ... and look for code using static blocks or static members.
您需要查看 Test.java 的代码并找出它导入的内容并确保这些类位于类路径中.如果您需要帮助跟踪类,您可以发布 Test.java 的代码.
You need to look at the code for Test.java and figure out what it imports and make sure those classes are on the classpath. You could post the code of Test.java if you need help tracking the classes down.
这篇关于NoClassDefFoundError 的可能原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!