I have 2 different Java projects, one has 2 classes: dynamicbeans.DynamicBean2
and dynamic.Validator
.
On the other project, I load both of these classes dynamically and store them on an Object
class Form {
Class beanClass;
Class validatorClass;
Validator validator;
}
I then go ahead and create a Validator
object using validatorClass.newInstance()
and store it on validator
then I create a bean object as well using beanClass.newInstance()
and add it to the session.
portletRequest.setAttribute("DynamicBean2", bean);
During the lifecycle of the Form
project, I call validator.validate()
which loads the previously created bean object from the session (I'm running Websphere Portal Server). When I try to cast this object back into a DynamicBean2
it fails with a ClassCastException.
When I pull the object back out of the session using
faces.getApplication().createValueBinding("#{DynamicBean2}").getValue(faces);
and check the class of it using .getClass()
I get dynamicbeans.DynamicBean2
. This is the class I want to cast it to however when I try I get the ClassCastException.
Any reason why I'm getting this?
I had the same issue on a wildfly EJB, The EJB was returning a list of Objects and has an remote and a local interface. I used the Local interface by mistake what was working just fine up until the point you try to cast the objects in the list.
Local/Remote interface:
The EJB bean:
The correct spring wrapper around the EJB:
Note the $Remote, You can change this to $Local and it will find the Local interface just fine, and also execute the methods without any issue (from a separate application on the same container), but the model objects are not marshaled and are from a different class loader if you use the local interface by mistake.
Had the same
my.package.MyClass cannot be cast to my.package.MyClass
on WildFly 10.1 and, as I understand, I did the opposite to what @Emil Lundberg described in his answer.I have added the module (which contains
my.package.MyClass
) tomy.war/WEB-INF/jboss-deployment-structure.xml
as a dependencyand removed the corresponding jar from
my.war/WEB-INF/lib
, re-deployed the WAR and then the code worked as expected.Thus, we made sure it solves the issue. Now, we need to make sure the issue won't come back, for example, when the updated version of WAR will be assembled and deployed.
For this, in the sources of those WAR, it is required to add
<scope>provided</scope>
for those jar inpom.xml
, so that whenmy.war
is re-assembled next time with the fix/enhancement code injected, it will not bundle this jar intomy.war/WEB-INF/lib
.The class objects were loaded in different classloaders, therefore the instances created from in each of classes are seen as 'incompatible'. This is a common issue in a an environment where there are many different classloaders being used and objects are being passed around. These issues can easily arise in Java EE and portal environments.
Casting an instance of a class requires that the Class linked to the object being casted is the same as the one loaded by the current thread context classloader.
I had same problem with an EJB lookup from another EJB. I solved adding @Remote(MyInterface.class) to EJB class configuration
I had the same issue while using several JBoss instances on different machines. To bad I didn't stumble across this post earlier.
There were artifacts deployed on different machines, two of them declared class loaders with identical name.I changed one of the classloader names and everything worked fine => Beware of Copy&Paste!
Why doesn't the ClassCastException thrown mention the involved class loaders? - I think that would be very useful information.
Does anyone know if there will be anything like this available in the future? Needing to check the class loaders of 20-30 Artifacts is not that pleasant. Or is there something I missed in the exception text?
EDIT: I edited the META-INF/jboss-app.xml file and changed the name of the loader, the idea is to have a unique name. At work we use the artifact id(unique) combined with the version inserted by maven({$version}) during the build.
Using dynamic fields is only optional but helps if you want to deploy different versions of the same application.
You can find some info here: https://community.jboss.org/wiki/ClassLoadingConfiguration
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:
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.
Make sure the class is loaded by a class loader higher in the hierarchy.
Serialize and deserialize the object. (Yuck!)
There is probably a more appropriate way for your particular situation though.