ClassLoader question

Discussion in 'Java' started by pramodr, Apr 28, 2008.

  1. pramodr

    pramodr Guest

    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
    pramodr, Apr 28, 2008
    #1
    1. Advertising

  2. 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
    Leonard Milcin, Apr 28, 2008
    #2
    1. Advertising

  3. pramodr

    Mark Space Guest

    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/JDCTechTips/2001/tt0410.html

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

    http://javatechniques.com/blog/faster-deep-copies-of-java-objects/
    Mark Space, Apr 28, 2008
    #3
  4. pramodr

    Guest

    On Apr 28, 5:19 pm, 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.
    >
    > 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.
    , Apr 29, 2008
    #4
  5. pramodr

    Roedy Green Guest

    On Mon, 28 Apr 2008 07:19:37 -0700 (PDT), pramodr
    <> 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
    Roedy Green, Apr 29, 2008
    #5
  6. Roedy Green wrote:
    > On Mon, 28 Apr 2008 07:19:37 -0700 (PDT), pramodr
    > <> 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
    Leonard Milcin, Apr 29, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. Miguel

    classloader in tomcat

    Mr. Miguel, Jul 11, 2003, in forum: Java
    Replies:
    2
    Views:
    582
    Miguel De Anda
    Jul 14, 2003
  2. Replies:
    5
    Views:
    684
    Nigel Wade
    Jul 15, 2003
  3. Miguel De Anda

    ClassLoader or Class.newInstance

    Miguel De Anda, Jul 14, 2003, in forum: Java
    Replies:
    1
    Views:
    819
    John C. Bollinger
    Jul 15, 2003
  4. Philippe Poulard
    Replies:
    1
    Views:
    5,688
    Philippe Poulard
    Jul 22, 2003
  5. Emmanuel PIC

    ClassLoader problem

    Emmanuel PIC, Jul 30, 2003, in forum: Java
    Replies:
    3
    Views:
    401
    Emmanuel PIC
    Aug 4, 2003
Loading...

Share This Page