Re: Problem with ClassLoader

Roland Schemers (Roland.Schemers@Eng)
Tue, 10 Mar 1998 11:01:31 -0800 (PST)

From: Roland.Schemers@Eng (Roland Schemers)
Message-Id: <199803101901.LAA09487@crypto.eng.sun.com>
Subject: Re: Problem with ClassLoader
To: vgoenka@novell.com (Vishal Goenka)
Date: Tue, 10 Mar 1998 11:01:31 -0800 (PST)
In-Reply-To: <s5048d04.016@novell.com> from "Vishal Goenka" at Mar 10, 98 00:42:06 am

Its a feature. You can't cast to class X if the code trying to cast to
cast X is on the classpath and class X isn't. That is because when you
say:

X myX = (X) obj;

Then it looks for X using the same class loader as the code trying to
do the cast. What you need to do is either use reflection to invoke methods
on X, or create a common base class that all objects loaded through
the class loader can extend, and keep that class on classpath.

For example, if X extended java.lang.Runnable you could cast obj
to Runnable and then call the run method.

roland

>
> I'm writing my own class loader that subclasses java.lang.ClasLoader and =
> instantiates a class after extracting the byte codes defining the class =
> from some proprietary file format. In order to use the methods of this =
> class, I need to typecast the Object (instantiated using newInstance() on =
> the Class returned by my classloader's loadClass method) to its real class =
> name (say X), but the file X.class is not in the classpath, since it =
> doesn't exist in the first place. The class X was created by my classloader=
> , and I expect that the class definition is available to java runtime, =
> still, I get ClassDefNotFound error when typecasting the class as :
> X myX =3D (X)obj; // Assuming obj was the object created as discussed =
> above.
>
> Kindly help. I can produce sample code if the problem is not clear. Is =
> this a feature or a bug. Please elaborate.
>
> Thanks and regards,
> Vishal Goenka
> Novell
>
>