Wednesday, February 8, 2017

NoClassDefFoundError vs ClassNotFoundException




Here are few differences between NoClassDefFoundError vs ClassNotFoundException:

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 a host of reasons. One of them being multiple class loaders and visibility of class among them.

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.

No comments:

Post a Comment