Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ClassLoader

1 view
Skip to first unread message

eugen.w...@gft.de

unread,
Oct 1, 1999, 3:00:00 AM10/1/99
to
Dear People,

In my application I need some Java classes to be loaded dynamically,
and not only loaded, but reloaded if class files changed. As I found
out, Class.forName("MyClass") is no good in this case, since JVM loads
classes first from from it's internal cache and then from the disk.

So I decided to write my own class loader without this caching. I
extended ClassLoader and overrode its loadClass() method. Then I
invocked it like this:

MyClassLoader loader = new MyClassLoader();
Class c = Class.forName("MyClass", true, loader);
Object o = c.newInstance();

//till here everything is fine
//Now the problem comes:

MyClass mc = (MyClass)o; //fails miserably with ClassCastException

I have no clue what can cause such a strange behaviour. Please help me
if you can! My Email: eugen.w...@gft.de


Sent via Deja.com http://www.deja.com/
Before you buy.

Lee Fesperman

unread,
Oct 1, 1999, 3:00:00 AM10/1/99
to
eugen.w...@gft.de wrote:
> MyClassLoader loader = new MyClassLoader();
> Class c = Class.forName("MyClass", true, loader);
> Object o = c.newInstance();
>
> //till here everything is fine
> //Now the problem comes:
>
> MyClass mc = (MyClass)o; //fails miserably with ClassCastException

This is because the MyClass class referenced in the last line is not the one loaded by
your class loader. It is loaded automatically by the system. There are two solutions to
this:

+ Reference items loaded with your class loader with reflection ... meaning you access
MyClass indirectly rather than directly.

+ Use your class loader to load a class that then references MyClass. When you load a
class with a class loader, then references to classes such as your last line will use
your class loader to load them.

--
Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)

Biju Thomas

unread,
Oct 1, 1999, 3:00:00 AM10/1/99
to
eugen.w...@gft.de wrote:
>
> In my application I need some Java classes to be loaded dynamically,
> and not only loaded, but reloaded if class files changed. As I found
> out, Class.forName("MyClass") is no good in this case, since JVM loads
> classes first from from it's internal cache and then from the disk.
>
> So I decided to write my own class loader without this caching. I
> extended ClassLoader and overrode its loadClass() method. Then I
> invocked it like this:
>
> MyClassLoader loader = new MyClassLoader();
> Class c = Class.forName("MyClass", true, loader);
> Object o = c.newInstance();
>
> //till here everything is fine
> //Now the problem comes:
>
> MyClass mc = (MyClass)o; //fails miserably with ClassCastException
>

It may be because the JVM's default class loader has already loaded the
MyClass, and, since classes loaded by different class loaders are
distinct, you get the error. If that is the case, you need to find out
why the MyClass was loaded before, and, avoid such a situation.

--
Biju Thomas

wor...@my-deja.com

unread,
Oct 4, 1999, 3:00:00 AM10/4/99
to
Dear Folks,

thanks to your feedback I managed to solve the problem described
below with my custom class loader.

But now I have another one. There is a defineClass() method in
ClassLoader and it is the only way to convert an array of bytes to a
Class instance. The problem is: if I try to call it for the same class
name more than once I get:

Exception in thread "main" java.lang.LinkageError: trying to refine
class UserManager (bad class loader?)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
...

What's going on? JVM won't allow me to redefine a class? But this is the
only way I can "reload" it!

Please help me. Thank you in advance!

In article <37F500...@ix.netcom.com>,
Lee Fesperman <firs...@ix.netcom.com> wrote:


> eugen.w...@gft.de wrote:
> > MyClassLoader loader = new MyClassLoader();
> > Class c = Class.forName("MyClass", true, loader);
> > Object o = c.newInstance();
> >
> > //till here everything is fine
> > //Now the problem comes:
> >
> > MyClass mc = (MyClass)o; //fails miserably with ClassCastException
>

> This is because the MyClass class referenced in the last line is not
the one loaded by
> your class loader. It is loaded automatically by the system. There are
two solutions to
> this:
>
> + Reference items loaded with your class loader with reflection ...
meaning you access
> MyClass indirectly rather than directly.
>
> + Use your class loader to load a class that then references MyClass.
When you load a
> class with a class loader, then references to classes such as your
last line will use
> your class loader to load them.
>
> --
> Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
>

Joseph Millar

unread,
Oct 4, 1999, 3:00:00 AM10/4/99
to
On Mon, 04 Oct 1999 12:13:03 GMT, wor...@my-deja.com wrote:
[snip!]

> What's going on? JVM won't allow me to redefine a class? But this is the
> only way I can "reload" it!

That's correct. In order to reload a class, you must go to
a new instance of the classloader.

--Joe
________________________________________________________________
Joseph A. Millar <jmi...@mediaone.net>
"Very funny, Scotty! Now beam down my clothes!" - James T. Kirk

0 new messages