What is the difference between NoClassDefFoundError
and ClassNotFoundException
?
What causes them to be thrown? How can they be resolved?
NoClassDefFoundError
is a linkage error basically. It occurs when you try and instantiate an object (statically with "new") and it's not found when it was during compilation.
ClassNotFoundException
is more general and is a runtime exception when you try to use a class that doesn't exist. For example, you have a parameter in a function accepts an interface and someone passes in a class that implements that interface but you don't have access to the class. It also covers case of dynamic class loading, such as using loadClass()
or Class.forName()
.