Classcastexception Because of Classloaders

ClassCastException because of classloaders?

You cannot cast between class loaders. Class identity is composed of fully qualified name and the class loader. Check class identity crysis here.

Solution for the ClassCastException due to ClassLoader issue

AFAIK, no, you can't cast an object of a class loaded by one class-loader in another class loader.

  • One solution would be to create a "common" class-loader which loads the classes to be used by your custom classloaders. So in your case, you'd have a new classloader which would load the given class and your custom classloaders would extend this classloader.
  • Another solution would be to pass around the "serialized" state between the two classloaders. Serialize one instance to a byte array and reconstruct the object in the other classloader by de-serializing the object stream.

Different classloaders cause ClassCastException when persisting data via Spring

There doesn't need to be two different versions of the class for a class cast exception to appear. Even the same class definition is seen as two different classes when loaded by two distinct classloaders. Which seems to be the case here.

Unfortunately I am not familiar with the platforms you use, so I can't give more concrete advice than this: try to experiment with moving the jar containing your Test class to different places on your web app classpath, and/or reconfiguring the Spring and Jetty classloaders so that both delegate the loading of Test to the same parent classloader.

ClassCastException while I use custom classloader

If uncomment this line I see following output:......

Class sexy_cla = xloa.loadClass("sur.che.SexyClassForLoader");

sur.che.SexyClassForLoader was loaded by Xloader and because it has an interface sur.che.ISexyInterface then jvm will use the classloader of sur.che.SexyClassForLoader to load this interface.

But as for this statement:

System.out.println(ISexyInterface.class.getClassLoader());

because the class which contained main method was loaded by AppLoader So jvm will use AppLoader to load ISexyInterface.class.

 ISexyInterface local_sexy = (ISexyInterface) sexy_ob;

And obviously, the classLoader of this ISexyInterface is AppLoader, however the inner dependence (ISexyInterface) of SexyClassForLoader was loaded by xLoader. In other words, there are two ISexyInterface.class in jvm one loaded by Xloader another by App. And the sexy_ob implements the ISexyInterface.class loaded by Xloader. You try to convert sexy_ob into a ISexyInterface type(loaded by App).
so castException.

If commented, there is only one ISexyInterface .class which loaded by App . So everything is ok.

ClassCastException when casting to the same class

I am not quite following your description of the program flow, but usually when you get ClassCastExceptions you cannot explain you have loaded the class with one classloader then try to cast it to the same class loaded by another classloader. This will not work - they are represented by two different Class objects inside the JVM and the cast will fail.

There is an article about classloading in WebSphere. I cannot say how it applies to your application, but there are a number of possible solutions. I can think of at least:

  1. Change the context class loader manually. Requires that you can actually get a reference to an appropriate class loader, which may not be possible in your case.

    Thread.currentThread().setContextClassLoader(...);
  2. Make sure the class is loaded by a class loader higher in the hierarchy.

  3. Serialize and deserialize the object. (Yuck!)

There is probably a more appropriate way for your particular situation though.

What is wrong with my classloader? ClassCastException when casting to the same class!

i think the system is complaining about an array being casted to a class
*L*com.model.A; cannot be cast to
com.model.A

please run the program in debug mode & see if you are getting an array in place of an object.

Getting class cast exception where both classes are exactly the same

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.



Related Topics



Leave a reply



Submit