NoClassDefFoundError on an interface, not a class

三世轮回 提交于 2019-12-18 08:46:56

问题


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?


回答1:


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.




回答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.



回答3:


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.




回答4:


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?




回答5:


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



回答6:


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



来源:https://stackoverflow.com/questions/6663810/noclassdeffounderror-on-an-interface-not-a-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!