What is ClassDefNotFoundException and NoClassDefFoundError and explain differences between these two?
Typically, NoClassDefFoundError
is thrown if the class failed to initialize.
When a class is used for the 1st time, it's being initialized. if that fails for whatever reason, the relevant exception is thrown. http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4
When the class is used again, it's known to be corrupt. Initialization won't be attempted again. JVM throws the mysterious NoClassDefFoundError
. Not really descriptive of the problem, could be quite confusing for diagnosis. It would be better if something like ClassInitFailedError
is thrown.
When you see a NoClassDefFoundError
, you should search the log backwards, find the root cause why the class failed to initialize (hopefully it's logged)
There is no ClassDefNotFoundException in standard Java lib, so I don't know what you are talking about.
ClassNotFoundException
is thrown when the class cannot be found in the classpath. NoClassDefFoundError
happens when class cannot be instantiated by new
because the jvm failed to initialize the class. It could be the constructor threw exceptions, or static block threw up, or version mismatch.
Core Java - What is ClassDefNotFoundException and ...
http://www.geekinterview.com/question_details/47825
精彩评论