4 Difference between NoClassDefFoundError and ClassNotFoundException

In this tutorial , we will understand the difference between NoClassDefFoundError  and ClassNotFoundException. This is one of the questions to increase the knowledge of java exceptions.
We have already shared the

We will look into the differences , similarities and examples of NoClassDefFoundError and ClassNotFoundException.


Difference between NoClassDefFoundError and ClassNotFoundException

Error vs Exception :  The major difference is in their names. You see that NoClassDefFoundError is Error and derived from LinkageError. It means that NoClassDefFoundError occurs during linking and program can’t run, but compiles successfully.
ClassNotFoundException is Exception and derived from ReflectiveOperationException
It occurs during runtime
Irrecoverable vs recoverable:  NoClassDefFoundError refers irrecoverable situation that are not being handled by try/catch/finally block. ClassNotFoundException is checked exception, which requires handling using try/catch/finally block.



Situations when throw and how to avoid them: Situation, when arise ClassNotFoundException, is 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.
To avoid this exception need to make sure that:
  • class is available in the logical class path of the class loader associated with the class;
  • class Loader API is used properly .ie whether a wrong class Loader is engaged in Class.forName();
  • dependent class of the class being loaded is visible to the class  loader;
  • etc.
Situation, when arise NoClassDefFoundError, is 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 (bad format of the class, the version number of a class not matching, etc.).
Class can’t be loaded on the following reasons:
  • failure to load the dependent class; 
  • the dependent class has a bad format; 
  • the version number of the class is incorrect;
  • the class been loaded already in a different classloader;
  • etc.

Examples of NoClassDefFoundError and ClassNotFoundException

If we have the following code: 

public class NoClassDefFoundErrorExample {
    public static void main(String[] args) {
        A a = new A(); 
    }
}
public class A extends B {
}

public class B {
}

Now if we remove B.class after compiling from a classpath.

 The result executing this code is: Exception in thread "main" java.lang.NoClassDefFoundError: B.

NoClassDefFounDifference between NoClassDefFoundError and ClassNotFoundException in JavadError



public class ClassNotFoundExceptionExample {
    public static void main(String[] args) throws ClassNotFoundException {
        Class.forName("com.example.Class1");
    }
}

The result executing this code is: 
Exception in thread "main" java.lang.ClassNotFoundException: com.example.Class1
Because Class Loader can’t load com.example.Class1 class.


Difference between NoClassDefFoundError and ClassNotFoundException in Java







Recap  : Difference between ClassNotFoundException and NoClassDefFoundError




ClassNotFoundExceptionNoClassDefFoundError
Exception vs ErrorCheckedExceptionError
Phase throwRuntimeLinking
DerivedReflectiveOperationException LinkageError
Irrecoverable vs Recoverable RecoverableIrrecoverable
Need try/catch/finally blockYesNo
Situation throw An application tries to load in a class
through its string name , but no
definition for the class with the specified
name could be found
ClassLoader instance tries to load
in the  definition of a class and
 no definition of the class could be found



Summary: 

If we look at oracle docs http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html, we can find:
“If an error occurs during class loading, then an instance of a subclass of LinkageError must be thrown at a point in the program that (directly or indirectly) uses the class or interface being loaded.
If the Java Virtual Machine ever attempts to load a class C during verification (§5.4.1) or resolution (§5.4.3) (but not initialization (§5.5)), and the class loader that is used to initiate loading of C throws an instance of ClassNotFoundException, then the Java Virtual Machine must throw an instance of NoClassDefFoundError whose cause is the instance of ClassNotFoundException.”

From this explanation we can make conclusion that ClassNotFoundException is a root cause of NoClassDefFoundError and NoClassDefFoundError is a special case of type loading error, that occurs at Linking step.





About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry