java 接口上的 NoClassDefFoundError,而不是类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6663810/
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
NoClassDefFoundError on an interface, not a class
提问by Marcus
I have an issue with NoClassDefFoundError
. I am using interfaces, and no class definition should be available:
我有一个问题NoClassDefFoundError
。我正在使用接口,并且没有可用的类定义:
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.
实现这个接口的类编译没有任何错误,并且已经构建了一个 JAR 文件,但是在运行时它给了我一个错误。
import ...;
import code.*;
public class MultiDoc extends LanguageAnalyser implements Constants{}
Constants
contains only a list of constants.
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.
我读了一些指向 CLASSPATH 的帖子作为这个问题的原因,但我的code
CLASSPATH 中有这个包。如果我没有它,它将产生编译错误。所以,问题应该是别的。
The runtime error is:
运行时错误是:
java.lang.NoClassDefFoundError: code/Constants
What's the solution?
解决办法是什么?
回答by zacheusz
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.
检查这个类的静态初始化。看这里:NoClassDefFoundError 和 ClassNotFoundException 之间有什么区别。java.lang.NoClassDefFoundError: code/Constants
并不意味着 Constants 类不在 CLASSPATH 中。事实上,情况恰恰相反。这意味着 ClassLoader 找到了该类,但是在尝试加载该类时,它在读取类定义时遇到了错误。这通常发生在所讨论的类具有静态块或使用类加载器未找到的类的成员时。
回答by Hot Licks
Note that there are two different exceptions that sound very similar: ClassNotFoundException and NoClassDefFoundError.
请注意,有两个不同的异常听起来非常相似:ClassNotFoundException 和 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.
第一个异常发生在 JVM 查找“packageB.ClassA”但在搜索路径中找不到的简单情况下。我怀疑还有其他一些情况,但相当罕见。
The second exception occurs mostly when an appropriately-named class file isfound, but for some reason it can't be used. There are two primary reasons for this:
当一个合适的名字命名的类文件大多发生第二异常的发现,但由于某种原因不能使用。这有两个主要原因:
- The class is in, eg, "packageA/ClassA.class" but internally is named "packageB/ClassA" (or the package name was accidentally omitted in the source, or the class file is in the wrong directory, given its package name).
- While loading the class, there was some sort of error, usually an error initializing static storage.
- 类在例如“packageA/ClassA.class”中,但在内部被命名为“packageB/ClassA”(或者包名在源代码中被意外省略,或者类文件在错误的目录中,给定了它的包名) .
- 加载类时,出现某种错误,通常是初始化静态存储时出错。
回答by Andreas Dolk
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:
从您的问题中,我看到编译后的Constants
接口驻留在一个 jar 中,该 jar 与实现类的 jar/位置不同。所以你应该能够像这样执行你的应用程序:
java -cp /path-to-jar/JarContainingConstants.jar my.application.Main
(replace the names with your real names)
(用你的真实姓名替换名字)
Ifyou've added the other classes to another jar and then ifyou made that jar executable and then ifyou tried to run it with java -jar MyApplication.jar
, thenany the classpath defined outside the executed jar is ignored. But the above command shoud work with any jar (executable or not).
如果您已将其他类添加到另一个 jar,然后如果您使该 jar 可执行,然后如果您尝试使用 运行它java -jar MyApplication.jar
,则忽略在执行的 jar 之外定义的任何类路径。但是上面的命令应该适用于任何 jar(可执行与否)。
from comment
来自评论
[alef@localhost ~]$ java -cp /../code-misc.jar /.../MultiDoc.jar
Exception in thread "main" java.lang.NoClassDefFoundError: /.../MultiDoc/MultiDoc/jar
Caused by: java.lang.ClassNotFoundException: .home.
... .MultiDocDateMathcer.jar
at java.net.URLClassLoader.run(URLClassLoader.java:217)
...
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: /.../MultiDoc/MultiDocr.jar.
Program will exit.
You call is definitely incorrect. Do it like this (UPDATE):
你打电话肯定是不对的。这样做(更新):
java -Dgate.home=/my/new/gate/home/directory -cp gate.jar;code-misc.jar;MultiDoc.jar gate.Main
回答by Joachim Sauer
If you have somedirectory/code
in your classpath, then that's wrong. You alwaysneed the basedirectory in your classpath (in this case it would be somedirectory
). Java itself will search those roots for a directory called code
containing a file called Constants.class
.
如果您somedirectory/code
的类路径中有,那是错误的。您始终需要类路径中的基目录(在本例中为somedirectory
)。Java 本身将在这些根目录中搜索code
包含名为Constants.class
.
回答by morja
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?
编译时您可能将它放在 CLASSPATH 中,但这并不能保证它在您运行时在 CLASSPATH 中。你如何运行它?
回答by Andrew Zaitsev
Any Interface must declared inside class.
任何接口都必须在类中声明。
public class Calbacks {
public interface IBaseFragmentInterface {
void NotifyMainActivity();
}
}
*I very long find resolution of this problem, but i find resolution independently by the method of scientific poking
*我很早就找到了这个问题的解决方案,但我通过科学戳的方法独立找到了解决方案