Java 获取两个类完全相同的类转换异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2371967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:39:08  来源:igfitidea点击:

Getting class cast exception where both classes are exactly the same

javaclasscastexception

提问by Phil

I am doing a JBoss SEAM project and when I view a form I get this error.

我正在做一个 JBoss SEAM 项目,当我查看表单时出现此错误。

java.lang.ClassCastException:
it.cogitoweb.csi.entity.csiorelav.CsiTipoLav cannot be cast to
it.cogitoweb.csi.entity.csiorelav.CsiTipoLav

Its alway the same JPA class which is related to the form which is shown on the screen, it doesn't make sense to me why is it the same class, it seems impossible.

它总是与屏幕上显示的表单相关的相同 JPA 类,对我来说为什么它是同一个类没有意义,这似乎是不可能的。

采纳答案by Joachim Sauer

This happens when two different ClassLoaderobjects load classes with the same name. The equality of two classes in Java depends on the fully qualified name andthe class loader that loaded it.

当两个不同的ClassLoader对象加载具有相同名称的类时,就会发生这种情况。Java 中两个类的相等性取决于完全限定名称加载它的类加载器。

So if two independent class loaders load classes from the same location, then objects of those types will not be able to be cast to each others type, even if their classes are called the same.

因此,如果两个独立的类加载器从同一位置加载类,那么这些类型的对象将无法转换为彼此的类型,即使它们的类调用相同。

回答by skaffman

This is because the class has been loaded by two different classloaders. You cannot cast between them.

这是因为该类已被两个不同的类加载器加载。你不能在他们之间施法。

You've likely got a duplicate copy of CsiTipoLavin your application, and the two different copies are being loaded at different times from different classloaders. JBoss has a plethora of different classloaders in a hierarchy, and it's easy to get things in a twist.

CsiTipoLav的应用程序中可能有一个重复的副本,并且这两个不同的副本在不同的时间从不同的类加载器加载。JBoss 在一个层次结构中有大量不同的类加载器,很容易把事情搞得一团糟。

Make sure you only have one copy of the class.

确保您只有一份该课程的副本。

回答by Thorbj?rn Ravn Andersen

The object you are trying to cast, is loaded by a different classloader than the one which has loaded the class you are trying to cast into.

您尝试转换的对象由与加载您尝试转换的类的类加载器不同的类加载器加载。

回答by shaktimaan

As Joachim explained earlier, java.lang.ClassCastException typically occurs when two classloaders load the classes with the same name. However, i have come across another situation when this could occur.

正如 Joachim 之前解释的那样,java.lang.ClassCastException 通常发生在两个类加载器加载同名类时。但是,当这可能发生时,我遇到了另一种情况。

This could occur with some IDE's that automatically reloads classes that have been modified. In such cases there might be older versions of the class retained in memory causing ClassCastException.

某些 IDE 会自动重新加载已修改的类,这可能会发生这种情况。在这种情况下,可能会在内存中保留旧版本的类,从而导致 ClassCastException。

Here are a few ways you could resolve this issue :

以下是您可以解决此问题的几种方法:

  1. If you are writing a custom class loader, while loading a class make sure that the base/default class loader does not already have an instance of that class loaded.

  2. Make the class being loaded a sub-class of the class that is already loaded by the default class loader.

  3. Make the class being loaded implement an interface that is already loaded by the default class loader.

  1. 如果您正在编写自定义类加载器,则在加载类时确保基类/默认类加载器尚未加载该类的实例。

  2. 使正在加载的类成为默认类加载器已加载的类的子类。

  3. 使被加载的类实现一个已经被默认类加载器加载的接口。

More info here - http://www.jspwiki.org/wiki/A2AClassCastException

更多信息在这里 - http://www.jspwiki.org/wiki/A2AClassCastException

回答by Patrick P

In my case i had two different *.ear and wanted to load a class from the other. So i had to isolate the classloader. I used this description:

就我而言,我有两个不同的 *.ear 并且想从另一个加载一个类。所以我不得不隔离类加载器。我使用了这个描述:

http://www.thorgull.be/wiki/index.php?title=ClassLoader_isolation_in_JBOSS

http://www.thorgull.be/wiki/index.php?title=ClassLoader_isolation_in_JBOSS

It worked for me.

它对我有用。