How to HotSwap Java Code into JVM (Redefinition Example)

Java HotSwap Overview

The terms of development is actually writing code, testing and fixing bugs with frequency, the most of common thing we want is that the modified java classes to be effective immediately without restarting the application. In this tutorial, we guide how Java classes can be reloaded without dynamic class loaders, and shows example step-by-step that reloads changes to JVM on-the-fly without downtime, we provide an ‘embedded’ software development solution that hotswap Java code.

Early in 2002, Sun introduced a Redefine (also known as “HotSwap”) technology into the Java 1.4 JVM, that allows modified class bytecode to be updated into running JVM through the debugger APIs, This dynamic class redefinition helps the developer to substitute an old class instance with a new one, this meant that all existing objects and variables will use and execute new codes instead of old ones when these methods were invoked. It’s very power functionality to improve developer’s convenience and productivity, let developers have ability to do fix-and-continue debugging.

By now, basically all IDE models have supported this functionality. The author of this article even tested with Eclipse, IDEA and netbeans, the result is positive, the stability is well the performance is in terms of second speeds.

The example hotswaping java code to JVM in Eclipse.


Please install JDK, eclipse, assuming you created a web project with deployed in tomcat.

1.  Edit your application’s startup file, to add the following line as start-up parameter, then start the application.

set DEBUG_OPTS= -hotspot -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=5555,server=y,suspend=n -Djava.compiler=NONE

2). Download the java hotswap example zip file, it includes a hotswap Ant XML and additional Java libraries.

3). Config Hotswap Java code Ant Task.

Import the Java source code of the project to eclipse, navigate to external tools configurations ->add ant build, specifics the location of XML file as buildfile as below screen, in Arguments text-box, add variables -Dresource.file-Dhost.port and -Dproject.dir.

Please switch to Targets Tab of external tools configurations, choose target “hotswap” to execute:

Continue to switch to Classpath tab to add hotswap.jar as user entries:

Attached Hotswap XML:

<?xml version="1.0"?>

<project name="HOTSWAP" default="hotswap" basedir=".">
    <property name="project.dir" value="${project.dir}"/>
    <property name="" value="${}"/>
    <property name="host.port" value="${host.port}"/>
    <property name="project.lib" value="${project.dir}/lib/"/>

    <!-- Set classpath -->
    <path id="app.classpath">
        <fileset dir="${project.dir}/build/lib"/>
    <taskdef name="hotswap" classname="dak.ant.taskdefs.Hotswap"/>

	<target name="compile">
		<javac destdir="${project.dir}/build/out"
		    <src path="${project.dir}/src"/>
			<classpath refid="app.classpath"/>

	<target name="hotswap">
			<format property="class.tstamp" pattern="MM/dd/yyyy kk:mm:ss" />
		<antcall target="compile"/>

		<echo message="Start hotswap ${}:${host.port}"/>
		<hotswap verbose="true" host="${}" port="${host.port}">
			<fileset dir="${project.dir}/build/out" includes="**/**.class">
				<date datetime="${class.tstamp}" pattern="MM/dd/yyyy kk:mm:ss" 
when="after" granularity="0"/>

The main and default ant target name is hotswap, it will call JPDA to replaces classes on a running JVM. This task can take the following arguments:

  • verbose – prints class names as they are being swapped
  • failonerror – causes task to exit if any error occurs
  • host – the host of the JVM to be attached to (defaults to localhost)
  • port – the port number to be used to attach to the JVM
  • name – if not using a socket connection, this name is the share memory label

Of these arguments,  3 of 5 items has been configed in screenshot 1 to pass into eclipse ant task.

4. Just Run it, to hotswap the Java code to the running application.

Click on the button ‘Hotswap’ in eclipse to start hotswap Java code.

From the console of output, it shows we have hotswaped successfully.

Unfortunately, the redefinition is limited to only change the body of method — it cannot apply to add either method, fields or otherwise. but Good news from JavaOne 2010: A modification of the Java HotSpot(TM) VM that allows unlimited class redefinition at runtime will become soon, it may be a nice feature in next JDK release.

Оцените статью
Добавить комментарий

Your email address will not be published. Required fields are marked *