Can NoClassDefFoundError be thrown AFTER the class has been loaded/used?

馋奶兔 提交于 2020-01-17 11:13:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!