What is NoClassDefFoundError in Java - cause and solution

NoClassDefFoundError in Java
NoClassDefFoundError is a common error in Java which occurs if ClassLoader is not able to find the class while loading it. As per Javadoc, NoClassDefFoundError can be thrown during linking or loading of the 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 a 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

NoClassDefFoundError in Java can come due to a variety of reasons in Java, But most of the time java.lang.NoClassDefFoundError comes due to CLASSPATH errors in Java. A 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.

What is NoClassDefFoundError in Java2) 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 the common reason which causes NoClassDefFoundError in Java. Two things which are most important to troubleshoot this error is knowledge of Classpath and ClassLoader in Java.

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

1 comment:

  1. What is difference between NoClassDefFoundError and a ClassNotFoundException? I guess both comes due to missing class in CLASSPATH? Why Java has two types of error for same reasons?