Help! -- different class loader issue
From within an EJB I am trying to cast a serializable object, that was passed into this EJB, to its original type. The process ended with a ClassCastException, even though I have double checked that the object being casted is of the correct type and fully qualified package path. It turns out that the problem is caused by the involvment of 2 different class loaders -- The class loader that loads the object is not the same one that loads the EJB doing the casting. The thing that confuses me the most is that this program used to work fine without the exception when it was running in an older environment. Is this a VM issue? Do we have control over what class loader to use when load certain classes/objects? What's the fix to the problem?
Please help!
Thanks in advance.
Lifeng
[815 byte] By [
xulifeng] at [2007-9-26 1:19:26]

It is a Java platform version issue. Since Java 2 The classloaders are lay out in a hierarchy. You can read about it in the public chapters of the book "Inside the Java 2 Virtual Machine" by Bill Venners at
www.artima.com
This may be helpful for you . It specifies the class loaders used by a thread to load subsequent classes: aThread.setContextClassLoader(aClassLoader)
Other soulution is to have the object and EJB being loaded by the same class loader, which could be a common parent class loader for the ones that in your code are actually asked to load these objects
Otherwise, java.lang.reflect can deal with objects whose type is not known by the compiler.
Thanks Botella,
Your comments sound very interesting. What I would like to know more about is that when you say "Other soulution is to have the object and EJB being loaded by the same class loader", how exectly do you do this? In our app, we have our EJBs deploied within a JAR file. Do you think we can still load our regular objects, together with our EJBs, with ONE loader? and how? If you could point me to the correct direction by giving me some simple example, or docs describing this, or anythig, that would be very very helpfull for us...
Thank you very much.
Lifeng