Suppose we have a trivial Java program consisting of just one class:
public class HelloWorld { private static void replacable(int i) { System.out.println("Today is a nice day with a number " + i); } public static void main(String[] args) throws Exception { for(int i = 0; i < 100000; ++i) { replacable(i); Thread.sleep(500); } }
After compiling and running, the output will be as follows:
Today is a good day with number 0
Today is a good day with number 1
Today is a good day with number 2
Today is a good day with number 3
...
My question is: is there (or is there on the horizon) a way to exchange the replacable method at runtime? Something like writing another version of HelloWorld with a new version of replacable , compiling it, and then the old version in an already running JVM?
So, if I write a new version as follows:
private static void replacable(int i) { System.out.println("Today is an even nicer day with a number " + i); }
there is something similar to replacing Erlang hot code where I can do this:
- run the original program
- write a modified version
- using the command line, connect to the running JVM and replace the existing method
so at runtime this will happen:
Today is a good day with number 15000
Today is a good day with number 15001
Today is even nicer day with number 15002
Today is an even more pleasant day with number 15003
...
Suppose the above program is standalone, runs in the standard Java SE environment, there is nothing else in the classpath, so it's almost a Hello Hello style program.
Note. I know that technologies such as bytecode manipulation ( cglib ), aspectJ , jRebel , JMX , there is a hot swap of methods in Java EE, etc., but thatβs not what I was thinking about. Think of Erlang.