Reputation: 423
Context: java.io.File class has a static inner class method as follows:
LazyInitialization.temporaryDirectory();
[EDITED to add some more code] My code below eventually calls the above line of code. An exception is thrown from within the temporaryDirectory() method, which in my context is fine/expected.
try {
File tempFile = File.createTempFile("aaa", "aaa");
} catch (Exception e) {
// handle exception
}
Then, when I next invoke the same method (createTempFile) again, I get a "java.lang.NoClassDefFound error - Could not initialize class java.io.File$LazyInitialization"
Question: I assumed that the inner class LazyInitialization should have been loaded by the class loader when its static method was invoked, even though the inner method threw an exception. Yet, why am I seeing the NoClassDefFound error when invoking the second time? Is the original assumption incorrect?
Upvotes: 4
Views: 1706
Reputation: 22446
When a static initialization code throws a runtime exception, it is wrapped by ExceptionInInitializerError and thrown in the context of the code triggering the class loading (if its an Error exception, it is not wrapped). At this point, the class failed loading. Therefore, any attempt to use it later will cause a NoClassDefFoundError.
Perhaps this is what you experience.
Upvotes: 7