changing code while program is running  
Author Message
blacky





PostPosted: 2005-4-8 19:39:00 Top

java-programmer, changing code while program is running Hello,

I would like to have my class code loaded into application, which is
already running. I tried something like this:

public class Main
{

...


private void reloadClass() throws Exception
{
new Pair("","").print(); //class to be reloaded
System.in.read (); //interval - here comes manual class recompilation
ClassLoader.getSystemClassLoader ().loadClass ("Pair"); //reload class
new Pair("","").print(); //check if new version loaded
}
}

It seems that ClassLoader.loadClass checks the presence of the Pair
class in memory and doesn't relod it. How to omit it?

regards

blacky
 
Matt Humphrey





PostPosted: 2005-4-8 20:38:00 Top

java-programmer >> changing code while program is running
"blacky" <email***@***.com> wrote in message
news:d35qmc$964$email***@***.com...
> Hello,
>
> I would like to have my class code loaded into application, which is
> already running. I tried something like this:
>
> public class Main
> {
>
> ...
>
>
> private void reloadClass() throws Exception
> {
> new Pair("","").print(); //class to be reloaded
> System.in.read (); //interval - here comes manual class recompilation
> ClassLoader.getSystemClassLoader ().loadClass ("Pair"); //reload class
> new Pair("","").print(); //check if new version loaded
> }
> }
>
> It seems that ClassLoader.loadClass checks the presence of the Pair
> class in memory and doesn't relod it. How to omit it?

You can't. Even if you could, there is no way to automatically migrate the
objects of the earlier class to be instances of the new class because there
is no way to ensure they are compatible.

What I think you can do, however (I havn't tried this), is to use the same
technique that app servers use to reload web app servlets (also JServ and
JUnit)without reloading the JVM . To do this, first don't let your classes
be loaded by the system class loader. Instantiate and use a separate one.
When you want to reload a class, discard (or cease to use) the objects of
the original class, unload the old class loader, instantiate a new
classloader (with a unique id / instance) and use this new class loader to
load the class again. The loaded class will be distinct from the prior
version. Then reload your data using whatever migration policy you need to
have. (E.g. reload from database, re-read XML, etc.) Presuming no
properties were changed, it's not clear to me if you can deserialize data to
the new class. (Changing properties will definately break serialization.)

There may be better ways to do this.

http://archives.java.sun.com/cgi-bin/wa?A2=ind0003&L=servlet-interest&D=0&P=
109117

http://www.artima.com/insidejvm/ed2/

Cheers,
Matt Humphrey email***@***.com http://www.iviz.com/


 
blacky





PostPosted: 2005-4-9 21:37:00 Top

java-programmer >> changing code while program is running Thank you for the answer.

regards

blacky