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 the program can't run but compiles successfully. ClassNotFoundException is Exception and derived from ReflectiveOperationException. It occurs during runtime.

Irrecoverable vs recoverable

NoClassDefFoundError refers to irrecoverable situations that are not being handled by try/catch/finally block. ClassNotFoundException is a checked exception, which requires handling using try/catch/finally block.

Situations when they are thrown and how to avoid them

The situation, when arising ClassNotFoundException, is an application trying to load in a class through its string name using:

1). the forName method in class Class.

2). the findSystemClass method in class ClassLoader.

3). 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 take care of the following things:

class is available in the logical classpath 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.

When NoClassDefFoundError occurs?

It occurred when 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 for the following reasons:

1). failure to load the dependent class.

2). the dependent class has a bad format.

3). the version number of the class is incorrect.

4). the class has been loaded already in a different classloader etc.

Example of NoClassDefFoundError

If we have the following code:

public class NoClassDefFoundError1 { 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.NoClassDefFoundError1: B.

Example of ClassNotFoundException

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

The result executing this code is: Exception in thread "main" java.lang.ClassNotFoundException: com.javacodestuffs.Hello Because Class Loader can't load com.javacodestuffs Hello.

Post/Questions related to Difference Between NoClassDefFoundError And ClassNotFoundException


Java try-catch Examples
Try with Resources statement in Java
Checked and Unchecked Exceptions in Java

In this article, we have seen the Difference Between NoClassDefFoundError And ClassNotFoundException.