NoClassDefFoundError vs ClassNotFoundExcepiton in Java

Both NoClassDefFoundError and ClassNotFoundException are dreaded Error which comes when JVM or ClassLoader not able to locate class during class loading process. Since different ClassLoader loads classes from different location, sometime this issue may caused because of incorrect CLASSPATH as well i.e. some JAR files from lib is mission or from old version. Though look quite similar there is subtle difference between NoClassDefFoundError and ClassNotFoundException, NoClassDefFoundError indicates that class was present during time of compilation but not available when you run Java program, some time error on static initializer block can also result in NoClassDefFoundError. On the other hand ClassNotFoundException is nothing to do with compile time, ClassNotFoundException comes when you try to load a class in runtime using Reflection, e.g. loading SQL drivers and corresponding Class loader is not able to find this class. Apart from this fundamental difference between NoClassDefFoundError and ClassNoFoundException in Java, let's see some more points for better understanding.

NoClassDefFoundError vs ClassNotFoundException

Difference between NoClassDefFoundError and ClassNotFoundException in Java
Here are few more difference between both of them in point form :

1) NoClassDefFoundError is an Error which is unchecked in nature, i.e. doesn't require try-catch or finally block. On the other hand ClassNotFoundException is a checked Exception and requires mandatory handing using either try with catch block or try with finally block, failure to do so will result in compile time error.

2) If you are experiencing NoClassDefFoundError in J2EE environment, there could be host of reason, one being multiple class loader and visibility of class among them. See 3 ways to solve NoClassDefFoundError for more details.

3) Often java.lang.ClassNotFoundException is thrown as result of following method call, Class.forName(), ClassLoader.findSystemClass() and ClassLoader.loadClass().

4) Another difference between NoClassDefFoundError and ClassNotFoundException is that NoClassDefFoundError is a LinkageError and can come during linking, while java.lang.ClassNotFoundException is an Exception and occurs during runtime.

That's all on difference between NoClassDefFoundError vs ClassNotFoundException in Java. Just remember these list of difference while debugging or troubleshooting NoClassDefFoundError or ClassNotFoundException, this will reduce confusion and help to solve problem quickly.

Other Java Interview questions explanation from Java67


  1. Both ClassNotFoundException and NoClassDefFoundError are caused by missing classes in CLASSPATH, usually due to missing JAR files, but if JVM initiate the process due to transitive reference and failed to load the class at runtime it throws java.lang.NoClassDefFoundError, while if your code explicitly tries to load the classes e.g. by using Class.forName() method and class is not present in CLASSPATH, then JVM throws java.lang.ClassNotFoundException. You will often find a NoClassDefFoundError actually caused by ClassNotFoundException, you can see it by looking for "Caused By : " word in error message.