Meaning of java.lang.ClassCastException: someClass incompatible with someClass

五迷三道 提交于 2019-11-27 02:04:46

Philippe Riand explaned this by email:

This class cast happens because the same class had been loaded twice by 2 different class loaders. Thus, from a Java standpoint, they are different and the cast fails.

Now, each XPages application is having its own classloader. But this class loader is discarded each time a design change happens to the application, through Domino Designer for example. This is required as a change to an XPages generates a new Java class that should then be loaded instead of the previous one. When this happens, the classloader is discarded and a new is created. Then all the application related classes are reloaded, as they are needed, even though they didn't change.This is a common behavior implemented by J2EE servers. That said, if your code is caching an object in a scope that is not discarded when a design change occurs, then this is likely to happen. For example, the applicationScope & sessionScope are currently not discarded when a design change happens, which might lead to this problem. This was a design choice as discarding the scopes sometimes provides a bad developer experience, but with this drawback.

Finally, saving faces-config.xml works as a workaround. When this file is saved, then the entire module is discarded from memory, including the scopes, This explains why it works. Making a change to your custom Java class should reload the module and remove the issue.

So it seems putting beans (even indirectly) into sessionScope or applicationScope is the cause.

If the same class file is loaded in different class loaders, the two resulting Java classes are not the same class; you wouldn't be allowed to pass instances of one to functions expecting the other. Generally, if you're seeing this kind of problem, it's because you've got multiple child classloaders that can access a jar file that's not visible to their common parent classloader. You may need to move the jar containing "someclass" to a common library directory instead of (for example) a specific webapp directory.

Just putting my experience over here.

I was running my app on CAT environment with multiple JVMs when I came across this issue. Because the same build was running successfully for me on ITG environment, I restarted both the JVMs on CAT and the error was resolved. Not exactly sure what was causing it.

Cleaning the project also make this works!

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