Getting class cast exception where both classes are exactly the same
Asked Answered
H

5

27

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

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.

Heist answered 3/3, 2010 at 14:19 Comment(0)
I
42

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

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.

Isotron answered 3/3, 2010 at 14:22 Comment(7)
I'm having a similar problem. Is there any way around this?Phratry
@Joachim Sauder: How can this issue be fixed, is there any way around this?Archway
Solution is: interface usage as intermediator.Stepsister
@msangel: that only helps if you can make sure that the interface is loaded by a single classloader only, and if you can manage that, you can usually just as easily make sure that the objects are loaded with the same classloader as well.Isotron
What is the solution!?Restivo
@Joachim Sauer Can you provide solution as java guru?Gillman
In my case, I had an ear in JBOSS with different wars and jars. The common beanutils library was telling me class X is not assignable from class X. Knowing this was a classloader issue, I was able to work around the problem by setting the maven dependencies' <scope>provided</scope> in the jars and war poms, while not doing that in the ear pom. We also had a similar case were the class in conflict was available as a JBoss module; in that case we set the scope to provided everywhere.Felton
C
8

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.

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.

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.

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

Cementation answered 27/9, 2011 at 10:24 Comment(1)
Yes that was bloody IDE X( . Thanks a lot eclipse almost took my life.(production day)Aleedis
L
5

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 CsiTipoLav in 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.

Make sure you only have one copy of the class.

Luella answered 3/3, 2010 at 14:22 Comment(0)
I
2

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.

Insurer answered 3/3, 2010 at 14:22 Comment(0)
B
2

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:

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

It worked for me.

Binnings answered 30/8, 2013 at 9:7 Comment(1)
I have faced the same issue. The url provided seems to be not available now. Any way, can you please explain or let me know any steps... A BIG HELP for me...Debauchery

© 2022 - 2024 — McMap. All rights reserved.