ClassLoader question

P

pramodr

Hi group,

I am new to class loader in Java and would like to explore this topic.
My question is whether it is possible to "reload" a class dynamically
which has already been loaded by the default class loader. The class
is put in the CLASSPATH.

I heard that custom class loaders are used to dynamically load
classes. Is there a limitation for this in this case ?

Please anybody help

TIA
Pramod
 
L

Leonard Milcin

pramodr said:
Hi group,

I am new to class loader in Java and would like to explore this topic.
My question is whether it is possible to "reload" a class dynamically
which has already been loaded by the default class loader. The class
is put in the CLASSPATH.

The answer is no.
I heard that custom class loaders are used to dynamically load
classes. Is there a limitation for this in this case ?

The limitation is the fact that you can't have the class in the
classpath. Since every classloader will ask the parent to load the
class, and system classloader is the ultimate parent of all classloaders
no classloader other than system classloader will ever have a chance to
load your class if it is in the classpath.

It is possible to reload classes at runtime but then you have to follow
some rules (don't use the class directly in your code) and set up some
infrastructure (don't put it in the CLASSPATH, use separate classloader,
use factory to create new instances, etc.).

See http://exampledepot.com/egs/java.lang/ReloadClass.html for an example.

Regards,
Leonard Milcin
 
M

Mark Space

Matt said:
Loading classes with a class loader is easy and it is reasonably easy to
establish some kind of new context (re-load a plugin or component) that uses
newer or different versions of classes than other concurrent contexts. Some
webservers (JBoss is one) do this so you can drop in a revised web app war /
ear and it will run with the new one. Note, however, that the old one is
closed out and requests go to the new one. What you can't do via
classloaders is change the class definition of an existing object. The JVM
debugging protocol does have that capability, but I don't think it's done
via classloaders. If you can convert your objects into an inactivate form,
such as an XML string or serialized data or so forth, they can be
reconstituted into a new class definition via a new classloader. I haven't
had a chance to try that yet but it sounds like fun.

Interesting idea. Serialization can also be used to implement deep
copying on objects (the subject of recent discussion here). It's slow,
but there's ways to speed it up. Combine serialization/copy with
classloading, and you can mutate existing classes into completely new ones.

Here's Sun's article on deep copying via serialization:

http://java.sun.com/developer/JDCTechTips/2001/tt0410.html

Here's a faster version of the deep copy algorithm:

http://javatechniques.com/blog/faster-deep-copies-of-java-objects/
 
N

newsmaestro

Hi group,

I am new to class loader in Java and would like to explore this topic.
My question is whether it is possible to "reload" a class dynamically
which has already been loaded by the default class loader. The class
is put in the CLASSPATH.

I heard that custom class loaders are used to dynamically load
classes. Is there a limitation for this in this case ?

Please anybody help

TIA
Pramod

Check out this site:

http://javagoldmine.by.ru

Look in ClassLoader Code and ClassLoader Experts chapters.
You'll find plenty of useful information.
 
L

Leonard Milcin

Roedy said:
Yes, but you need a new classloader to do it.

That's not correct. Once the class is loaded by the system classloader
no other classloader will ever have a chance to load that class. That's
simple implication of two facts: that every classloader will first
attempt to look it up using parent classloader and that system
classloader is the ultimate parent of all classloaders.

So if you want to have the ability to reload the class you can't put the
class in the classpath in the first place.

Best regards,
Leonard Milcin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top