tags:

views:

622

answers:

8

why we need to restart tomcat server when a class file is changed,is there no other way?

+3  A: 

There certainly is! Start Tomcat in development mode, then each webapp will restart itself upon being redeployed.

From the Tomcat docs:

The servlet which implements Jasper is configured using init parameters in your global $CATALINA_BASE/conf/web.xml.

...

development - Is Jasper used in development mode (will check for JSP modification on every access)? true or false, default true.

There are settings you can change to adjust what exactly Tomcat will look for to check for updates. I usually deploy individual class files to their appropriate directory under WEB-INF/classes and then

touch WEB-INF/web.xml

to kick-start a restart of the application; I think web.xml is one of the files Tomcat checks by default.

Carl Smotricz
I may be wrong but this seem to be taken from the *Jasper 2 JSP Engine How To* (http://tomcat.apache.org/tomcat-6.0-doc/jasper-howto.html) and applies to JSPs, not classes.
Pascal Thivent
Naw, you're right as usual ;)
Carl Smotricz
+8  A: 

You can configure Tomcat and make your webapp "reloadable". To do so, add reloadable=true to the <Context> element of your webapp. About the reloadable attribute, the documentation says:

Set to true if you want Catalina to monitor classes in /WEB-INF/classes/ and /WEB-INF/lib for changes, and automatically reload the web application if a change is detected. This feature is very useful during application development, but it requires significant runtime overhead and is not recommended for use on deployed production applications. That's why the default setting for this attribute is false. You can use the Manager web application, however, to trigger reloads of deployed applications on demand.

Pascal Thivent
* sigh * Give my votes to Pascal please, his is the more useful and comprehensive answer. Looking at his, it seems that development mode only checks for changes in JSPs, which isn't what the OP asked about.
Carl Smotricz
A: 

If you're using WAR files to deploy, you can set autoDeploy=true in the Tomcat config, which causes Tomcat to watch the web application root ("webapps" by default) for new or changed WAR files. If such a file is found, it is automatically deployed.

As Pascal Thivent said, though, you can use the Tomcat Manager application (/manager/html) to start, stop, deploy, and undeploy specific applications. If the files you're changing are in a specific application, this is a good way to get Tomcat to recognize the changes.

spork
+1  A: 

If you develop, your IDE should be able to do this transparently on a suitable server. E.g. the Dynamic Web Project in Eclipse knows how to talk to Tomcat.

If you deploy, then create WAR-files and deploy those. Tomcat knows how to redeploy a WAR-file.

Thorbjørn Ravn Andersen
A: 

Check out JRebel.

Greg Mattes
A: 

On a more general note, the reason you have to do this is because in Java, when a classloader loads a class, it cannot unload it. What Tomcat has to do is use a new classloader and reload all the classes it needs.

Kelly French
A: 

Besides setting autoDeploy=true in server.conf , you also should be careful not to put any classes in the shared classloader. Classes which are loaded by the shared class loader cannot be re-loaded.

Nick Hristov
A: 

You can use tools JRebel. See example on my blog http://przemek-nowak.pl/2010/10/jrebel-czyli-jak-przyspieszyc-prace-programisty/. The example concern Tomcat 6.0

przemek