NoClassDefFoundError 发生在接口上,而不是类上

发布于 2024-11-19 18:13:17 字数 569 浏览 1 评论 0原文

我遇到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夕嗳→ 2024-11-26 18:13:18

任何接口都必须在类内部声明。

public class Calbacks {
    public interface IBaseFragmentInterface {
        void NotifyMainActivity();
    }
}

*我很早就找到了这个问题的解决方案,但我通过科学戳的方法独立找到了解决方案

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

幽梦紫曦~ 2024-11-26 18:13:17

检查该类的静态初始化。看这里: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.

猥琐帝 2024-11-26 18:13:17

请注意,有两个听起来非常相似的不同异常:ClassNotFoundException 和 NoClassDefFoundError。

第一个异常发生在 JVM 查找“packageB.ClassA”但在搜索路径中找不到它的简单情况中。我怀疑还有其他一些案例,但相当罕见。

第二个异常主要发生在找到适当命名的类文件,但由于某种原因无法使用它时。造成这种情况的主要原因有两个:

  1. 该类位于“packageA/ClassA.class”中,但内部被命名为“packageB/ClassA”(或者在源代码中意外省略了包名称,或者类文件位于错误的目录,给定其包名称)。
  2. 加载类时,出现某种错误,通常是初始化静态存储时出错。

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:

  1. 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).
  2. While loading the class, there was some sort of error, usually an error initializing static storage.
美人骨 2024-11-26 18:13:17

如果您的类路径中有 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 be somedirectory). Java itself will search those roots for a directory called code containing a file called Constants.class.

我不在是我 2024-11-26 18:13:17

编译时您可能会将其放在 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?

宁愿没拥抱 2024-11-26 18:13:17

从您的问题中我看到编译的Constants接口驻留在一个jar中,该jar与实现类的jar/位置不同。因此,您应该能够像这样执行您的应用程序:(

java -cp /path-to-jar/JarContainingConstants.jar my.application.Main

用您的真实姓名替换名称)

如果您已将其他类添加到另一个 jar 中,那么如果您使该 jar 可执行,然后如果您尝试使用 java -jar MyApplication.jar 运行它,那么则在执行的 jar 外部定义的任何类路径被忽略。但上面的命令应该适用于任何 jar(可执行或不可执行)。


来自评论

[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$1.run(URLClassLoader.java:217) 
  ... 
  at java.lang.ClassLoader.loadClass(ClassLoader.java:266) 
Could not find the main class: /.../MultiDoc/MultiDocr.jar. 
Program will exit.

你的电话肯定是不正确的。这样做(更新):

java -Dgate.home=/my/new/gate/home/directory -cp gate.jar;code-misc.jar;MultiDoc.jar gate.Main

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:

java -cp /path-to-jar/JarContainingConstants.jar my.application.Main

(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

[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$1.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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文