问题
The context is this: I have class with all static methods and a static initialize block. This class is part of a web application, which is packed as a war and deployed inside WebSphere 10.
There are situations where other classes in the war try to call methods on this class, only to get a NoClassDefFoundError. What I find strange is that, I can see from the logs that the class's methods and such have already been called multiple times, successfully, but all of a sudden, an identical (previously successful) method call will presently generate a NoClassDefFoundError. At this point any future such calls to the class's static methods will generate that error (until the WebSpehere container is restarted or the war is redeployed).
What puzzles me is why the class seems to work for a while before throwing the NoClassDefFoundError. The documentation on this exception says:
The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.
So my question is,how come a class is usable for a while, clearly responding to calls to its static methods and then throw NoClassDefFoundError?
回答1:
NoClassDefFoundError
its not ClassNotFoundException
. it can occur when:
Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.
My experience shows that if there an exception in static initializer block, this exception can be thrown too.
Why NoClassDefFoundError caused by static field initialization failure?
来源:https://stackoverflow.com/questions/19289205/can-noclassdeffounderror-be-thrown-after-the-class-has-been-loaded-used