Sunday, August 19, 2012

What is NoClassDefFoundError in Java - cause and solution

NoClassDefFoundError in Java
NoClassDefFoundError is a common error in Java which occurs if ClassLoader does not able to find the class while loading it. As per Javadoc NoClassDefFoundError can be thrown during linking or loading of class file. It's denoted by java.lang.NoClassDefFoundError and comes when that particular class is present during compile time but some how not available during runtime. People often confuse between ClassNotFoundException and NoClassDefFoundError in Java which is also related to class loading in Java but there is slight difference between NoClassDefFoundError and ClassNotFoundException in Java, Later comes when Java tries to load a class by its binary name during runtime and Class is not available at that time. for example when you load JDBC driver using Class.forName(String classname) , you will get java.lang.ClassNotFoundException instead of NoClassDefFoundError.



Common cause of NoClassDefFoundError in Java

What is NoClassDefFoundError in Java
NoClassDefFoundError in Java can come due to variety of reasons in Java, But most of the time java.lang.NoClassDefFoundError comes due to CLASSPATH errors in Java. Correct understanding of CLASSPATH is required to troubleshoot and solve NoClassDefFoundError in Java. Anyway here are some of the common reason which results in NoClassDefFoundError in Java :

1) Offender Class is not available in Classpath in Java.
2) Offender Class is available in Java but not visible to ClassLoader which is trying to load that particular class.
3) some shell script or environment variable is overriding value of CLASSPATH environment variable, it may be Class-Path attribute defined in JAR file or other CLASSPATH environment variable define later in shell script.

That's all on What is NoClassDefFoundError in Java and What are common reason which cause NoClassDefFoundError in Java. Two things which are most important to troubleshoot this error is knowledge of Classpath and ClassLoader in Java.


Reference:
3 ways to solve NoClassDefFoundError in Java
Difference between NoClassDefFoundError and ClassNotFoundException in Java

No comments:

Post a Comment

Java67 Headline Animator