简体   繁体   English

class MyClass 无法转换为 class MyClass(MyClass 位于加载程序 org.glassfish.[...].WebappClassLoader@1 的未命名模块中)

[英]class MyClass cannot be cast to class MyClass (MyClass is in unnamed module of loader org.glassfish.[…].WebappClassLoader@1)

Why this error sometimes in deploy phase appens in Glassfish / Payara application server?为什么 Glassfish / Payara 应用程序服务器中有时会在部署阶段出现此错误? I can guess that the application server is trying to use two different classes of two different classloaders but is there a way to prevent it to do this behavior?我可以猜测应用程序服务器正在尝试使用两个不同类加载器的两个不同类,但是有没有办法阻止它执行这种行为?

I tried to lookup some source online whitout finding nothing.我试图在网上查找一些来源,但一无所获。

Edit: this happens on the same application in time of redeploy.编辑:这在重新部署时发生在同一个应用程序上。 It gets solved by a restart of the application server but obliviously this is not a solution它可以通过重新启动应用程序服务器来解决,但不知不觉这不是解决方案

java.lang.ClassCastException: class com.MyClass cannot be cast to class com.MyClass (com.MyClass is in unnamed module of loader org.glassfish.web.loader.WebappClassLoader@1, com.MyClass is in unnamed module of loader org.glassfish.web.loader.WebappClassLoader@2)

Last Edit, after the great response of Stephen C.最后编辑,在斯蒂芬 C 的巨大反应之后。 What are the tools to undestand why Payara/GC doesn't destroy the old object?有什么工具可以解释为什么 Payara/GC 不破坏旧的 object?

I can guess that the application server is trying to use two different classes of two different classloaders but is there a way to prevent it to do this behavior?我可以猜测应用程序服务器正在尝试使用两个不同类加载器的两个不同类,但是有没有办法阻止它执行这种行为?

Yes, that's what I think is happening.是的,这就是我认为正在发生的事情。 If identical .class files are loaded by different classloaders, the resulting runtime types are different and cannot by cast.如果相同的.class文件由不同的类加载器加载,则生成的运行时类型是不同的,并且不能通过强制转换。

There are three ways to avoid this:有三种方法可以避免这种情况:

  1. Don't pass or share these objects between different webapps.不要在不同的 webapps 之间传递或共享这些对象。

  2. Move the JARs that define the classes that need to be shared into the web container's shared library area... so that they are loaded by the webcontainer's classloader rather than the webapp classloader(s).将定义需要共享的类的 JARs 移动到 web 容器的共享库区域...以便它们由 webcontainer 的类加载器而不是 webapp 类加载器加载。

  3. If the classes need to be loaded by multiple webapp classloaders (eg because the have the same name but different implementations) you may need to rearchitect your application so that the classes implement a common interface that is loaded by a single classloader.如果类需要由多个 webapp 类加载器加载(例如,因为它们具有相同的名称但实现不同),您可能需要重新架构您的应用程序,以便类实现由单个类加载器加载的公共接口。 If your webapp code then only casts to the shared interface, you won't run into this problem.如果你的 webapp 代码只转换到共享接口,你就不会遇到这个问题。


What if the web app is the same?如果 web 应用程序相同怎么办? (so when the application redeploy the same app) (所以当应用重新部署同一个应用时)

If that is the case, then it sounds like the problem is that your webapp's shutdown code is not doing the right thing.如果是这种情况,那么听起来问题在于您的 webapp 的关闭代码没有做正确的事情。 Java objects created by the earlier deployment of the webapp are leaking into the later one. Java 由早期部署的 webapp 创建的对象正在泄漏到后面的对象中。

  • Check that something is not caching application objects.检查某些东西是否没有缓存应用程序对象。
  • Check that application objects are not hiding in session state or thread-locals, or something like that.检查应用程序对象是否没有隐藏在 session state 或 thread-locals 或类似的东西中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM