, ,

I came across the below two kind of exceptions in my application. The first one is ClassNotFoundException and the other one is NoClassDefFoundError. Initially, I was thinking that both has same meaning. After that I realized both error messages are literally same, it has different meaning in it.


The name itself mean that, the particular class is not found by the classloader. It is a subclass of java.lang.Exception. You may have many reasons for it like,

1. The class is missing from the classpath.
2. The class in question is trying to be loaded from another class which was loaded by different classloader (visibility problem in classloader hierarchy).

The possible places where we get such error are, loading 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 the ClassLoader
but no definition for the class with the specified name could be found in classpath.

As we know that if a client can reasonably be expected to recover from an exception, we will make it as a checked exception. The ClassNotFoundException is checked exception so that we can give different implementation if we get any exception in loading class using reflection (Eg: ClassLoader.loadClass(className) ). In most of the cases, this exception come when we use the method Class.forname.


It is a subclass of java.lang.Error. This exception is thrown when the JVM or classloader instance tries to load in the definition of a class (as part of normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found. It occurs when the source was successfully compiled, but at runtime, the required class files were not found. This may be something that can happen in the distribution or production of JAR files, where not all the required class files were included.

One thing to be cleared here is, for ClassNotFoundException, the class was not found when you use reflection and facing different classloader issues. But NoClassDefFoundError comes when creating an object in normal way but the class definition is not found by the JVM or classloader.

It is unchecked exception since you may not know in which situation you will get such an error. You may have plenty of classes in your application and among them some are not loaded by classloader when you use new expression from your application. So the JVM terminates the process and throws runtime exception. This is definately not recoverable and your application will not even get a chance to try to recover.