102

I'm new to Tomcat. We have a dev machine with about 5 apps running. Even though it's dev, it's used by our clients pretty heavily during testing.

So say we need to make one small change on one class file. Right now, we have to shutdown Tomcat (affecting the other four apps), delete the WAR file (and web app directory), redeploy the new WAR file and restart Tomcat.

Of course, this upsets a few people because it destroys all logged in sessions for all apps.

Is there a better way to do this? I mean, is there a way to only reload the CLASS that changed instead of everything on the dev machine?

Thanks.

3
  • 1
    Change your session management to something that persists to file? Sorry couldn't resist :)
    – rogerdpack
    Commented Oct 11, 2017 at 15:49
  • 2
    @rogerdpack I've learned a great deal about Tomcat since I asked this question over six years ago. But thanks anyway.
    – cbmeeks
    Commented Oct 12, 2017 at 12:16
  • Regarding hot swapping just classes, see stackoverflow.com/questions/32459047/…
    – rogerdpack
    Commented Oct 12, 2017 at 16:43

3 Answers 3

67

Have you tried to use Tomcat's Manager application? It allows you to undeploy / deploy war files without shutting Tomcat down.

If you don't want to use the Manager application, you can also delete the war file from the webapps directory, Tomcat will undeploy the application after a short period of time. You can then copy a war file back into the directory, and Tomcat will deploy the war file.

If you are running Tomcat on Windows, you may need to configure your Context to not lock various files.

If you absolutely can't have any downtime, you may want to look at Tomcat 7's Parallel deployments. You may deploy multiple versions of a web application with the same context path at the same time. The rules used to match requests to a context version are as follows:

  • If no session information is present in the request, use the latest version.
  • If session information is present in the request, check the session manager of each version for a matching session and if one is found, use that version.
  • If session information is present in the request but no matching session can be found, use the latest version.
6
  • That's a good suggestion too. I never heard of the manager app until today. Yes, we use Windows. One concern I have with deleting the WAR file and waiting is that our clients are expecting "small fixes" frequently. Would those suggestions work for that scenario?
    – cbmeeks
    Commented Jul 5, 2011 at 15:08
  • 1
    @cbmeeks - It depends on how often these small fixes happen, and how long before someone notices. In either case, there will be a small amount of time that the application is unavailable. It depends on your hardware / application how long that time is. But it's measured in seconds, I'd say usually 5-10 on our applications. But your mileage may vary :)
    – Steve K
    Commented Jul 5, 2011 at 17:04
  • 1
    Wow. I've learned so much since I asked this question. Yeah, we now deploy Tomcat apps without taking Tomcat completely down. One thing I did was make sure all logging (app and Tomcat) do not record logs INSIDE the webapps/application folder. Or, configuring Tomcat to not lock also works. So we put logging in a central place outside of Tomcat's folder. Works like a dream. But your answer was the most helpful. Thanks!
    – cbmeeks
    Commented Mar 27, 2013 at 12:41
  • 6
    When redeploying without restart, you'll have to be careful with memory leaks - very often the classloaders will leak references and you need to restart the whole Tomcat process in order to free memory from the old version of the application. LiveRebel (mentioned in another answer) can automate all that for you. Commented Jun 10, 2013 at 14:57
  • 4
    You don't need to delete the war to redeploy. Just copy over the old war and wait few seconds.
    – peceps
    Commented Mar 26, 2014 at 15:16
37

There are multiple easy ways.

  1. Just touch web.xml of any webapp.

    touch /usr/share/tomcat/webapps/<WEBAPP-NAME>/WEB-INF/web.xml
    

You can also update a particular jar file in WEB-INF/lib and then touch web.xml, rather than building whole war file and deploying it again.

  1. Delete webapps/YOUR_WEB_APP directory, Tomcat will start deploying war within 5 seconds (assuming your war file still exists in webapps folder).

  2. Generally overwriting war file with new version gets redeployed by tomcat automatically. If not, you can touch web.xml as explained above.

  3. Copy over an already exploded "directory" to your webapps folder

2
  • 1
    Useful to know that the 'touch' method does not reload classes. IOW, it does not purge and reload the classloader hierarchy for the webapp
    – Rondo
    Commented May 29, 2016 at 1:10
  • 2
    Seeing why this works can read in $CATALINA_BASE/conf/context.xml. The WEB-INF/web.xml is watched for changes. Other resources can easily be listed there as needed.
    – Kevin
    Commented Jun 10, 2016 at 15:30
23

In conf directory of apache tomcat you can find context.xml file. In that edit tag as <Context reloadable="true">. this should solve the issue and you need not restart the server

1
  • Prefer the manager application instead. This is an expensive operation for the ram.
    – Sorter
    Commented Jan 3, 2019 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.