[ Index ] [ Individual Project ] [ Placement Presentation ] [ Timetable ]

[ Index ] [ Research ] [ Schedule ]

What does the JVM do when a class file that has already been loaded is replaced?

Last Updated: 1st January 2002

Eventually I want the tool to be able to upgrade the component on the fly, i.e. a running program can replace a certain package and then all future use of those classes run off the latest one. It isn't going to be a straight forward switch since the JVM loads the class into memory then what ever is on disk is irrelevant. Below is a short proof:

Code Listings

javac -classpath . Test.java
java -classpath . Test

I/O Trace:

d
Old Version
a
Old Version
d
Old Version
d
Old Version

Switch over to the latest version of the component:

mv Component.java Component.java.old
rm Component.class
mv Component2.java Component.java
javac -classpath . Component.java

First compile package one:

javac v1/com/milesbarr/test/Test.java
No problems.

I/O Trace:

a
Old Version
b
Old Version
d
Old Version
f
Old Version

Conclusion

Therefore any tool can't simply write over old versions of class files. Further research into how the JVM loads class files and if it has a mechanism to reload is needed. If I can't fiddle with the JVM directly then a special classloader that can be told to reload the classes by the JMX agent will need to be written.

Code Listings

Test.java:

import java.io.*;

public class Test {
    public static void main (String args[]) {
	Component c = new Component();

	while (true) {
	    BufferedReader console = new BufferedReader (new InputStreamReader(System.in));
	    
	    try {
		int chosen = console.read();
	    }
	    catch (IOException e) {
		e.printStackTrace();
	    }

	    c.print();
	}
    }
}

Component.java

public class Component {
    public void print () {
	System.out.println("Old Version");
    }
}

Component2.java

public class Component {
    public void print () {
	System.out.println("New Version");
    }
}


Miles Barr <
miles@milesbarr.com>