NoClassDefFoundError 发生在接口上,而不是类上
我遇到了 NoClassDefFoundError
的问题。我正在使用接口,并且不应有可用的类定义:
package code;
public interface Constants {...}
实现此接口的类编译时没有任何错误,并且已构建 JAR 文件,但在运行时它给了我一个错误。
import ...;
import code.*;
public class MultiDoc extends LanguageAnalyser implements Constants{}
Constants
仅包含常量列表。
我读到一些文章指出 CLASSPATH 是导致此问题的原因,但我的 CLASSPATH 中有 code
包。如果我没有它,就会产生编译错误。所以,问题应该是别的。
运行时错误是:
java.lang.NoClassDefFoundError: code/Constants
解决方案是什么?
I have an issue with NoClassDefFoundError
. I am using interfaces, and no class definition should be available:
package code;
public interface Constants {...}
The class implementing this interface compiles without any errors, and a JAR file has been built, but at runtime it gives me an error.
import ...;
import code.*;
public class MultiDoc extends LanguageAnalyser implements Constants{}
Constants
contains only a list of constants.
I read some posts pointing to CLASSPATH as a cause of this problem, but I have the code
package in my CLASSPATH. If I didn't have it, it would produce a compilation error. So, the problem should be something else.
The runtime error is:
java.lang.NoClassDefFoundError: code/Constants
What's the solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
任何接口都必须在类内部声明。
*我很早就找到了这个问题的解决方案,但我通过科学戳的方法独立找到了解决方案
Any Interface must declared inside class.
*I very long find resolution of this problem, but i find resolution independently by the method of scientific poking
检查该类的静态初始化。看这里:NoClassDefFoundError 和 ClassNotFoundException 之间有什么区别。
java.lang.NoClassDefFoundError: code/Constants
并不意味着 Constants 类不在 CLASSPATH 中。事实上恰恰相反。这意味着类加载器找到了该类,但是当尝试加载该类时,它在读取类定义时遇到了错误。当相关类具有使用类加载器未找到的类的静态块或成员时,通常会发生这种情况。Check the static initialization of this class. Look here: what is the difference between NoClassDefFoundError and ClassNotFoundException.
java.lang.NoClassDefFoundError: code/Constants
does not mean that the Constants class is not in the CLASSPATH. In fact it is 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.请注意,有两个听起来非常相似的不同异常:ClassNotFoundException 和 NoClassDefFoundError。
第一个异常发生在 JVM 查找“packageB.ClassA”但在搜索路径中找不到它的简单情况中。我怀疑还有其他一些案例,但相当罕见。
第二个异常主要发生在找到适当命名的类文件,但由于某种原因无法使用它时。造成这种情况的主要原因有两个:
Note that there are two different exceptions that sound very similar: ClassNotFoundException and NoClassDefFoundError.
The first exception occurs in the simple case that the JVM looks for "packageB.ClassA" and can't find it in the search path. There are a few other cases, I suspect, but fairly rare.
The second exception occurs mostly when an appropriately-named class file is found, but for some reason it can't be used. There are two primary reasons for this:
如果您的类路径中有
somedirectory/code
,那么这是错误。您总是需要类路径中的base目录(在本例中为somedirectory
)。 Java 本身将在这些根目录中搜索名为code
的目录,其中包含名为Constants.class
的文件。If you have
somedirectory/code
in your classpath, then that's wrong. You always need the base directory in your classpath (in this case it would besomedirectory
). Java itself will search those roots for a directory calledcode
containing a file calledConstants.class
.编译时您可能会将其放在 CLASSPATH 中,但这并不能保证运行时它也在 CLASSPATH 中。你如何运行它?
You might have it in your CLASSPATH when compiling but this does not guarantee that it is in the CLASSPATH when you run it. How do you run it?
从您的问题中我看到编译的Constants接口驻留在一个jar中,该jar与实现类的jar/位置不同。因此,您应该能够像这样执行您的应用程序:(
用您的真实姓名替换名称)
如果您已将其他类添加到另一个 jar 中,那么如果您使该 jar 可执行,然后如果您尝试使用 java -jar MyApplication.jar 运行它,那么则在执行的 jar 外部定义的任何类路径被忽略。但上面的命令应该适用于任何 jar(可执行或不可执行)。
来自评论
你的电话肯定是不正确的。这样做(更新):
From your question I see that the compiled
Constants
interface resides in a jar which is different from the jar/location of the implementing classes. So you should be able to execute your application like this:(replace the names with your real names)
If you've added the other classes to another jar and then if you made that jar executable and then if you tried to run it with
java -jar MyApplication.jar
, then any the classpath defined outside the executed jar is ignored. But the above command shoud work with any jar (executable or not).from comment
You call is definitely incorrect. Do it like this (UPDATE):