Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > ClassLoader question

Reply
Thread Tools

ClassLoader question

 
 
pramodr
Guest
Posts: n/a
 
      04-28-2008
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
 
Reply With Quote
 
 
 
 
Leonard Milcin
Guest
Posts: n/a
 
      04-28-2008
pramodr wrote:
> 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

--
Simplicity is the ultimate sophistication.
-- Leonardo da Vinci
 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      04-28-2008
Matt Humphrey wrote:

> 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/JDCTec...01/tt0410.html

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

http://javatechniques.com/blog/faste...-java-objects/
 
Reply With Quote
 
newsmaestro@gmail.com
Guest
Posts: n/a
 
      04-29-2008
On Apr 28, 5:19*pm, pramodr <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-29-2008
On Mon, 28 Apr 2008 07:19:37 -0700 (PDT), pramodr
<(E-Mail Removed)> 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
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Leonard Milcin
Guest
Posts: n/a
 
      04-29-2008
Roedy Green wrote:
> On Mon, 28 Apr 2008 07:19:37 -0700 (PDT), pramodr
> <(E-Mail Removed)> 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.


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

--
Simplicity is the ultimate sophistication.
-- Leonardo da Vinci
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ClassLoader problem Emmanuel PIC Java 3 08-04-2003 09:55 AM
How to find the ClassLoader with a static method ? Philippe Poulard Java 1 07-22-2003 02:32 PM
ClassLoader or Class.newInstance Miguel De Anda Java 1 07-15-2003 07:39 PM
ClassLoader + tomcat read-the-signature@send-spam-to-dev-null.com Java 5 07-15-2003 01:28 PM
classloader in tomcat Mr. Miguel Java 2 07-14-2003 06:18 PM



Advertisments