Tuesday, 23 June 2015

Difference between ClassNotFoundException vs NoClassDefFoundError in Java?

Main difference between them is in their root cause. 

ClassNotFoundExcpetion comes when you try to load a class at runtime by using Class.forName() or loadClass() and requested class is not present in classpath for example when you try to load MySQL or Oracle driver class and their JAR is not availabe.

While in case of NoClassDefFoundError requested class was present at compile time but not  available at runtime.


UnSupportedClassVersionError is easy to differentiate because it’s related to version of classpath and usually comes when you compile your code in higher Java version and try to run on lower java version. Can be resolved simply by using one java version for compiling and running your application.

No comments:

Post a Comment