Decreasing Your Development Time with Tomcat

How to recompile classes and edit JSPs without restarting Tomcat during development

Published: 14 Jan 2007

It seems like I always forget the magic combination for being able to get away with not reloading Tomcat for each class change/compilation. If for nothing else than to document it for myself here's the quick and dirty on how to set it up.

The environment here is Apache Tomcat 5.5.20 on Windows XP.

Add the following to ${TOMCAT_HOME}/conf/server.xml:

<Host name="YOUR_PROJECT_NAME_HERE"
         appBase="YOUR_PROJECT_ROOT_DIR"
         unpackWARs="true"
         autoDeploy="true"
         xmlValidation="false"
         xmlNamespaceAware="false">
</Host>

Note that you will have to change your local hosts configuration to have YOUR_PROJECT_NAME resolve to your local IP address. Under the expanded WAR directory for your project add a META-INF directory and add the following to a context.xml file in the META-INF directory:

<Context docBase="YOUR_WEB_ROOT_DIR"
         path="DESIRED_CONTEXT_PATH"
         reloadable="true"
         unloadDelay="3000"/>

For some reason I had to play with the value of the unloadDelay attribute in order to get the timing right for Tomcat to pick up changes correctly. It seemed as though Tomcat tried to re instantiate classes that were in the process of being recompiled. In the case above I have the delay set for 3000 ms (3 seconds).

In my Ant build.xml, I have a "redeploy" task set up to clean, recompile classes, and touch the web.xml file. Note that this task works on my expanded web project directory so the compiled classes are placed in the WEB-INF/classes directory or jarred up and placed in the WEB-INF/lib directory.

So with this setup you can now start Tomcat once and recompile classes and edit JSP's without having to constantly stop and start Tomcat between compiles. Although if you leave Tomcat running long enough, you may run into the known issue/circumstance of running out of memory. In this case, you may have to adjust your memory settings.