Restart tomcat when a class file is changed?

Viewed 29913

Why do we need to restart a tomcat server whenever a class file is changed, is there no other way?

8 Answers

Your question doesn't actually say whether you are concerned about a production downtime (i.e. reduce it by reloading the classes) or you want non-stop development. So I will try to clarify using the following points:

1) using <Context reloadable=true... in your catalina.home/conf directory you can make sure that your webapp reloads when a any class changes. You can add the resource change watchlist in <WatchedResources> element.

2) But this will reload the context, re-initialise the classloader, empty it's cache, and everything will be as if the webapplication has just started.

This approach will still leave your server unusable, because you have reloaded the Servlet's context. The true "Reload" is

1) You swap the byte code of the class, with some restrictions 2) JVM will return that class when "loadClass()" is called for that classloader.

This is java instrumentation. You can write your own agent which can hook into your JVM either at the beginning or in flight. However, you cannot define new method, and change static variables. These are JVM native restrictions (for Oracle HotSpot JVM, that I know of). You can use a different JVM e.g. DCEVM which doesn't have such restriction. So it's up to you how you want to handle your problem. If you know what you are doing (!), you can get away with replacing classes one-by-one. And you can even define a "Brand New Class", reference that class object/method in an existing/loaded class and instrument it to to pick up changes.

I hope this helps. All the answers here are what you need to make your decision.

Related