How to Hotswap classes in the running jvm?

One of the major pain of every server side java developer is that you have to redeploy your application every time you made a small change to the java classes. Now if you are working with a web application which consists 1500 classes, about 150 servlets(basically webservices), imagine how much time tomcat is gonna take to deploy or even worse redeploy that application.(it takes about 6 mins on my p4 pc). This was a small trick I learned from Masum Bhai at Therap.
JVM comes with a debugging api which actually lets you to change the defination of a class at runtime. These are the following steps you have to do.

1> Download hotswap.jar and put it in the “run-classpath” of your ant script.
2> JVM must start with debugging parameter. So add the following parameters at your java start command:

      -Xint -Xdebug -Xrunjdwp:transport=dt_socket,address=${hotswap.port},server=y,suspend=n

For example, if you are using tomcat, you can add this line at the beginning of the catalina.sh

export JAVA_OPTS="-Xint -Xms256m -Xmx256m -Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n"

alternatively , you can uset the “tomcat-start” ant task given below to start your tomcat. Just make sure you have the tomcat-home ant property defined.







 <!--enable timestamping upto second level (required for hotswapping)-->
     

       
     

 <!-- Use this when you want to see which files are being compiled -->
  





  

             
  

 

  
        

   
 





       
       
   

The rest is simple! When you change anything in a class/group of classes, just use “ant reload” and you will see the hotswap output like this:

ant reload
Buildfile: build.xml

prepare-common:

compile:
   [echo] Compiling the application source code....
  [javac] Compiling 1 source file to /home/sajid/projects/cmsstandaloneportal/,tmp/cmsstandaloneportal/WEB-INF/classes

reload:
[hotswap] hotswapping 1 files from /home/sajid/projects/cmsstandaloneportal/,tmp/cmsstandaloneportal/WEB-INF/classes
[hotswap] hotswapping com.x.y.z.p.web.struts.QDispatchAction

BUILD SUCCESSFUL
Total time: 10 seconds</pre>
10 seconds! Thats all you need!

I have used this trick with Tomcat, JBoss, and standalone java applications and every time it worked like a charm! I can’t tell how much deployment time it saved me. So No doubt its my most favorite ant task to date!

Note Of Caution:
Hotswapping doesn’t work when you change the structure of a class (add new method/delete method/rename method). It only works when you change the logic inside a method. Thats what we do most of the time anyway, isn’t it?