Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2008

Tip: Looking for answers? Try searching our database.

ClassLoader question

Thread view: 
pramodr - 28 Apr 2008 15:19 GMT
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
Matt Humphrey - 28 Apr 2008 15:49 GMT
> Hi group,
>
[quoted text clipped - 5 lines]
> I heard that custom class loaders are used to dynamically load
> classes. Is there a limitation for this in this case ?

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.

Matt Humphrey http://www.iviz.com/
Mark Space - 28 Apr 2008 18:47 GMT
> 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
[quoted text clipped - 8 lines]
> 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/
Leonard Milcin - 28 Apr 2008 16:09 GMT
> 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

Signature

Simplicity is the ultimate sophistication.
                                 -- Leonardo da Vinci

newsmaestro@gmail.com - 29 Apr 2008 08:14 GMT
> Hi group,
>
[quoted text clipped - 10 lines]
> 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.
Roedy Green - 29 Apr 2008 09:29 GMT
On Mon, 28 Apr 2008 07:19:37 -0700 (PDT), pramodr
<pramodvr@hotmail.com> wrote, quoted or indirectly quoted someone who
said :

>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.

Yes, but you need a new classloader to do it.  The code can even have
changed.  

see http://mindprod.com/jgloss/classloader.html
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Leonard Milcin - 29 Apr 2008 19:59 GMT
> On Mon, 28 Apr 2008 07:19:37 -0700 (PDT), pramodr
> <pramodvr@hotmail.com> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 6 lines]
>
> 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

Signature

Simplicity is the ultimate sophistication.
                                 -- Leonardo da Vinci

Kenneth P. Turvey - 30 Apr 2008 04:33 GMT
> 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.

Why not just write a classloader that doesn't behave like this?  You
don't have to defer to the system classloader, it is just what you do
when it makes sense.  

> 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.

Sure you can.

Signature

Kenneth P. Turvey <kt-usenet@squeakydolphin.com>



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.