How to solve java.lang.NoClassDefFoundError in java

What is hierarchy of java.lang. NoClassDefFoundError?

-java.lang.Object
-java.lang.Throwable
 -java.lang.Error
  -java.lang.LinkageError
   -java.lang.NoClassDefFoundError


NoClassDefFoundError is Checked (compile time exceptions) and UnChecked (RuntimeExceptions) in java ?

java.lang.NoClassDefFoundError  is a Error in java. Error and its subclasses are regarded as unchecked exceptions in java.


What is NoClassDefFoundError in java/ or what is No Class Definition Found Error ?

NoClassDefFoundError is thrown when JVM tries to load class but no definition of class could be found.
Or, you can simply say class was unavailable in classpath at runtime..


NoClassDefFoundError is thrown when JVM tries to load class which >
  • was NOT available at runtime but
  • was available at compile time.

Example/Programs/Scenarios where NoClassDefFoundError may be thrown in java>



  1. We will create very simple program which will throw NoClassDefFoundError .


NoClassDefFoundError is thrown when JVM tries to load class but no definition of class could be found.


First let’s write java class >
public class NoClassDefFoundErrorExample {
public static void main(String[] args) {
        new A(); // It can throw java.lang.NoClassDefFoundError
}
}
class A {
}


Let’s say above code in file =  E:\workspace\NoClassDefFoundErrorExample.java


Go to CMD and type below commands >


C:\Users\ankitmittal01>e:

E:\>cd E:\workspace

E:\workspace>javac NoClassDefFoundErrorExample.java

E:\workspace>


As soon as javac NoClassDefFoundErrorExample.java is called two .class files are formed.


Directory of .class files = E:\workspace\NoClassDefFoundErrorExample.class and
E:\workspace\A.class


Now, let's execute the above program >
E:\workspace>java NoClassDefFoundErrorExample
Program executed successfully!
Program was executed successfully!

Now, when above program can throw  NoClassDefFoundError?


Now, delete E:\workspace\A.class


Let’s say you have deleted A.class. Now, let's execute the program again >
E:\workspace>java NoClassDefFoundErrorExample
Exception in thread "main" java.lang.NoClassDefFoundError: : A
       at NoClassDefFoundErrorExample.main(NoClassDefFoundErrorExample.java:3)
Caused by: java.lang.ClassNotFoundException: A
       at java.net.URLClassLoader.findClass(Unknown Source)
       at java.lang.ClassLoader.loadClass(Unknown Source)
       at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
       at java.lang.ClassLoader.loadClass(Unknown Source)
       ... 1 more

E:\workspace>
As we see NoClassDefFoundError was thrown when JVM tries to load A.class which >
    • was NOT available at runtime but
    • was available at compile time.



  1. If any RuntimeException occurs in >
then NoClassDefFoundError may be thrown.


We will create another simple program to understand how RuntimeException occured in static block of class can throw NoClassDefFoundError.


First let’s write java class >
package noClassDefFoundErrorPkg;

public class NoClassDefFoundErrorExample {
public static void main(String[] args) {
    try {
        new A(); // It will throw java.lang.ExceptionInInitializerError
    } catch (Error error) {
        error.printStackTrace();
    }
   
    System.out.println("\n--------------\n");
   
    try {
        new A(); // It will throw java.lang.NoClassDefFoundError
    } catch (Error error) {
        error.printStackTrace();
    }
}
}
class A {
static {
    System.out.println("In static block of class A");
    System.out.println(1 / 0); // It will throw java.lang.ArithmeticException
}
/*
* Constructor of class A will never get executed
*/
A() {
    System.out.println("In constructor of class A");
}
}


Output of above program >
In static block of class A
java.lang.ExceptionInInitializerError
at noClassDefFoundErrorPkg.NoClassDefFoundErrorExample.main(NoClassDefFoundErrorExample.java:6)
Caused by: java.lang.ArithmeticException: / by zero
at noClassDefFoundErrorPkg.A.<clinit>(NoClassDefFoundErrorExample.java:27)
... 1 more
--------------
java.lang.NoClassDefFoundError: Could not initialize class noClassDefFoundErrorPkg.A
at noClassDefFoundErrorPkg.NoClassDefFoundErrorExample.main(NoClassDefFoundErrorExample.java:14)


In the above program, first new A() at line 6 throwed ExceptionInInitializerError because ArithmeticException occurred in initializing the class (i.e. in static block of class) then,
new A() at line 14 throwed NoClassDefFoundError because class initialization has already thrown error.


You must read ArithmeticException  is an unchecked exception and it is propagated automatically in java.



Now, we will create one more simple program to understand how RuntimeException occurred in initializing static variables of class can throw ExceptionInInitializerError and NoClassDefFoundError.


First let’s write java class >
package NoClassDefFoundErrorPkg2;
public class NoClassDefFoundErrorExample {
public static void main(String[] args) {
     try {
          new A(); // It will throw java.lang.ExceptionInInitializerError
     } catch (Error error) {
          error.printStackTrace();
     }
    
     System.out.println("\n--------------\n");
    
     try {
          new A(); // It will throw java.lang.NoClassDefFoundError
     } catch (Error error) {
          error.printStackTrace();
     }
}
}
class A {
static int i = 1 / 0; // It will throw java.lang.ArithmeticException
}


Output of above program >
java.lang.ExceptionInInitializerError
at NoClassDefFoundErrorPkg2.NoClassDefFoundErrorExample.main(NoClassDefFoundErrorExample.java:6)
Caused by: java.lang.ArithmeticException: / by zero
at NoClassDefFoundErrorPkg2.A.<clinit>(NoClassDefFoundErrorExample.java:24)
... 1 more
--------------
java.lang.NoClassDefFoundError: Could not initialize class NoClassDefFoundErrorPkg2.A
at NoClassDefFoundErrorPkg2.NoClassDefFoundErrorExample.main(NoClassDefFoundErrorExample.java:14)



  1. We will create another program which uses commons-lang.jar and will throw NoClassDefFoundError .


NoClassDefFoundError is thrown when JVM tries to load class but no definition of class could be found.


First let’s write java class >
import org.apache.commons.lang.StringUtils;
public class NoClassDefFoundErrorExample {
public static void main(String... arg) {
    System.out.println(StringUtils.class + " loaded");
    System.out.println(StringUtils.isNotEmpty("java")); //true
}
}


Let’s say above code in file =  E:\workspace\NoClassDefFoundErrorExample.java


Go to CMD and type below commands >


Set required jar (commons-lang.jar) in classpath
and then execute javac command.
C:\Users\ankitmittal01>e:

E:\>cd E:\workspace

E:\workspace>set CLASSPATH=.;E:\java jars\commons\commons-lang.jar

E:\workspace>javac NoClassDefFoundErrorExample.java

E:\workspace>


As soon as javac NoClassDefFoundErrorExample.java is called .class file is formed.


Directory of .class file = E:\workspace\NoClassDefFoundErrorExample.class


Now, let's execute the above program >
E:\workspace>java NoClassDefFoundErrorExample
class org.apache.commons.lang.StringUtils loaded
true
Program was executed successfully!

Now, when above program can throw  NoClassDefFoundError?


Now, either unset the the classpath by using command >
set CLASSPATH=
Or,
Delete or move E:\java jars\commons\commons-lang.jar


Let’s say you have unset the classpath. Now, let's execute the program again >
E:\workspace>set CLASSPATH=

E:\workspace>java NoClassDefFoundErrorExample
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils
       at NoClassDefFoundErrorExample.main(NoClassDefFoundErrorExample.java:5)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.StringUtils
       at java.net.URLClassLoader.findClass(Unknown Source)
       at java.lang.ClassLoader.loadClass(Unknown Source)
       at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
       at java.lang.ClassLoader.loadClass(Unknown Source)
       ... 1 more

E:\workspace>
As we see NoClassDefFoundError was thrown when JVM tries to load org.apache.commons.lang.StringUtils class which >
    • was NOT available at runtime but
    • was available at compile time.

Note : Jar used in above program = commons-lang.jar


How to avoid NoClassDefFoundError in java?


  1. Ensure that static initialization block of classes or static variables does not throw any RuntimeException.


  1. First of all you must ensure that class does not throws java.lang.ExceptionInInitializerError because that is likely to be followed by NoClassDefFoundError.


  1. You should ensure classpath has not changed just before execution of program i.e. after compilation of program.


  1. Ensure classpath environment variable is not overridden or changed.



RELATED LINKS>


Writing and executing first java program through CMD in windows, Setting up ECLIPSE in java, Directory of .class file



You must read ArithmeticException  is an unchecked exception and it is propagated automatically in java.

eEdit
Must read for you :