Change a method at runtime via a hot swap mechanism

You can do it via class loaders. For example if you are familiar with Servlet containers such as tomcat that reload pages as you modify them in development. Here is a great explanation of creating dynamic code in java. It not only explains loading but also compiling source on the fly. You should be able to apply the concepts covered to any strategy of reloading code you want to utilize.


You can either use the open-source HotSpot VM or the commercial JRebel IDE plugin to easily achieve your goal (view comparison table here).


I've used this hotswap ant task in many projects. The target java application can be launched via Ant, Eclipse, a command prompt, or any other means as long as it is launched in debug mode with the appropriate port open. The linked page provides instructions on how to do this via Ant.

Any number of classes can be hotswapped as long as the changes are not structural. Method body changes generally hotswap with ease. Code can be hotswapped by running an ant script via a shell or Eclipse.

At work, I utilize a script that hotswaps code changes automatically by comparing timestamps on the class files. This is similar to the sample on the project page that shows a simple example that only hotswaps changed classes.

Additional note: This utilizes the JPDA.


You can do this through the JPDA (Java Platform Debugger Architecture) interface: http://download.oracle.com/javase/1.4.2/docs/guide/jpda/enhancements.html#hotswap

It's not automatic as the Erlang case - the JVM doesn't monitor the class path for changes to class files then reload and relink them if changed, for fairly obvious reasons (Java was designed for web deployment you don't want to be polling a http URL for changes).