Java - ClassNotFoundException vs NoClassDefFoundException

There is a different between ClassNotFoundException and NoClassDefFoundException.

From Java

public class ClassNotFoundException
extends Exception

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.

public class NoClassDefFoundError
extends LinkageError

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.

In layman term, ClassNotFoundException occurs when your class is not found in the Java CLASSPATH. For example, the jar file that contains the class does not exist in the CLASSPATH

In the other hand, NoClassDefFoundException occurs when Java class loader found your class, however, it cannot find the definition that it is trying to load. Class definition can be anything declared within a class. For example, one of the class method is missing and some external class file is trying to access this missing method.






Comments

Popular Posts