Tuesday, March 8, 2016

Difference Between ClassNotFoundException Vs NoClassDefFoundError

ClassNotFoundException : 

ClassNotFoundException occurs when class loader could not find the required class in class path . So , basically you should check your class path and add the class in the classpath.

Thrown when an application tries to load in a class through its string name using:
  • The forName method in class Class.
  • The findSystemClass method in class ClassLoader.
  • The loadClass method in class ClassLoader.

 but no definition for the class with the specified name could be found.

This exception can be created using following program-

    import java.net.URL; 
    import java.net.URLClassLoader;
    public class ClassNotFoundExceptionTest {
        public static void main(String args[]) {

            try {

                URLClassLoader loader = new URLClassLoader(new URL[] { new URL(


            } catch (ClassNotFoundException e) {
            } catch (MalformedURLException e) {


To avoid this error , User should check the following -
  • Make sure that
         Class is available in the logical class path of the class loader associated with the class.        
  • Make sure that         
       Class Loader API is used properly .ie whether a wrong class Loader is engaged in Class.forName().
  • Make sure that
         dependent class of the class being loaded is visible to the class  loader.

NoClassDefFoundError :

This is more difficult to debug and find the reason. This is thrown when at compile time the required classes are present , but at run time the classes are changed  or removed or class's static initializes threw exceptions. It means the class which is getting loaded is present in classpath , but one of the classes which are required by this class , are either removed or failed to load by compiler .So you should see the classes which are dependent on this class .

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.
The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found. 

This error can be recreated using following test program –

public class NoClassDefFoundErrorTest {

     public static void main(String[] args) { 

        A a = new A(); 


public class A extends B { }

public class B { }

//Once you've compiled the above code remove the classfile for B. When 
//the code is executed, the following error occurs

//Exception in thread "main" java.lang.NoClassDefFoundError: B

Possible reasons of the error-
  • Bad format of the class
  • The version number of a class not matching
  • This can happen in the distribution or production of JAR files, where not all  the required class files were included.

User should check whether a Class is not available on the classpath(not missing in the jar file). This problem can occur also when Class cannot load .

There are various reasons the class could not be loaded. Possible reasons include the following:
  • Failure to load the dependent class
  • The dependent class has a bad format.
  • The version number of the class is incorrect. 

Contributors ©


Post a Comment


Get in touch with me


Bangalore, India