Re: Copy an Object from one ClassLoader to Another??

From: David Hilsee (
Date: 07/22/04

Date: Wed, 21 Jul 2004 21:52:11 -0400

"John Davison" <> wrote in message
> I have an Object that was created in a custom class loader. Since a
> Class is defined as the class name plus the class loader, a Class from
> one ClassLoader is not equivalent to a Class from another ClassLoader.
> For example:
> Class c1 = getClass().getClassLoader().loadClass( "com.acme.FooBar" );
> Class c2 = CustomClassLoader.loadClass( "com.acme.FooBar" );
> FooBar newC2 = (FooBar)c2; // This throws ClassCastException!
> You can not cast c2 to FooBar because the FooBar class loaded by the
> system ClassLoader is not equivalent to the FooBar class that created
> c2.

I think you left a call to newInstance() out. You cannot cast c2 to newC2
because c2 is an instance of Class and FooBar is not derived from it. I
just wanted to make sure that nobody gets confused.

David Hilsee