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?
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With