0 votes
1 view
in Java by (5.4k points)

What is the difference between NoClassDefFoundError and ClassNotFoundException?

What causes them to be thrown? How can they be resolved?

I often encounter these throwables when modifying existing code to include new jar files. I have hit them on both the client side and the server side for a java app distributed through webstart.

Possible reasons I have come across:

  1. packages not included in build.xml for the client side of code

  2. runtime classpath missing for the new jars we are using

  3. version conflicts with previous jar

When I encounter these today I take a trail-and-error approach to get things working. I need more clarity and understanding.

1 Answer

0 votes
by (66.4k points)

NoClassDefFoundError is a linkage error basically. This error occurs if you try and instantiate an object (statically with "new") and it is not found when it was while compilation. 

ClassNotFoundException error is more general and is a runtime exception if you try to use a class that is not present. Suppose, you have a parameter in a function that accepts an interface, and if you pass in a class that implements that interface but you don't have access to the class. It also occurs in the case of dynamic class loading, such as using loadClass() or Class.forName().

If you want to learn Java from the top experts, you can enroll in this Java Course program by Intellipaat.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !