You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tomcat 9应用启动与重启方法及自定义部署配置重复执行失效问题咨询

Tomcat 9: Startup/Restart Apps & Fix Your Deployment Auto-Reload Issue

Hi there! Let's tackle your questions one by one—starting with basic Tomcat operations, then diving into fixing your deployment and auto-reload problem.


1. How to Start & Restart Apps in Tomcat 9

Starting Tomcat

  • Command Line: Navigate to your Tomcat bin directory:
    • Windows: Run startup.bat (foreground) or catalina.bat start (background)
    • Linux/macOS: Run ./startup.sh (foreground) or ./catalina.sh start (background)
  • Windows Service: If you installed Tomcat as a Windows service, open the Services panel, find the Tomcat service, and click "Start".

Restarting Apps

  • Full Tomcat Restart: Stop Tomcat first with shutdown.bat/./shutdown.sh, then restart using the startup commands above. Or use catalina.bat restart/./catalina.sh restart for a one-step restart.
  • Reload Specific App via Manager Console:
    1. Enable the Tomcat Manager app (if not already enabled) by configuring users in conf/tomcat-users.xml.
    2. Access http://localhost:8080/manager/html, log in, find your app, and click Reload to restart it without stopping the entire server.

2. Fixing Your Deployment & Auto-Reload Problem

What's Causing the Issue?

When the mytestapp folder exists in webapps, Tomcat prioritizes this unpacked directory over the docBase war path you defined in server.xml. Tomcat's auto-deploy system automatically creates contexts for unpacked apps in webapps, which override your custom Context configuration.

Solution 1: Adjust server.xml Configuration

Modify your Host and Context settings to force Tomcat to use your specified war and config, and ignore the existing mytestapp directory:

<Host name="localhost" appBase="webapps" autoDeploy="true" deployOnStartup="true">
  <!-- Your custom context: points to the war in myapp_1, uses the external config -->
  <Context path="/mytestapp" 
           docBase="c:/Apache-tomcat/webapps/myapp_1/mytestapp.war" 
           debug="0" 
           reloadable="true" 
           override="true" 
           antiResourceLocking="true"
           unpackWARs="true">
    <Environment name="spring.config.location" 
                 value="file:c:/Apache-tomcat/webapps/myapp_1/application.properties" 
                 type="java.lang.String"/>
  </Context>
  <!-- Disable auto-deployment of the existing mytestapp directory -->
  <Context path="/mytestapp" docBase="webapps/mytestapp" reloadable="false" enabled="false"/>
</Host>
  • override="true": Forces your custom Context to override any auto-generated contexts for the same path.
  • antiResourceLocking="true": Fixes file locking issues on Windows that prevent replacing the war file.
  • unpackWARs="true": Ensures Tomcat unpacks the war to webapps/mytestapp and replaces the old directory when the war is updated.

Instead of modifying server.xml (which requires a Tomcat restart), create a separate context file in conf/Catalina/localhost/mytestapp.xml:

<Context docBase="c:/Apache-tomcat/webapps/myapp_1/mytestapp.war" 
         reloadable="true" 
         antiResourceLocking="true"
         unpackWARs="true">
  <Environment name="spring.config.location" 
               value="file:c:/Apache-tomcat/webapps/myapp_1/application.properties" 
               type="java.lang.String"/>
</Context>

This approach:

  • Lets you deploy/modify the context without restarting Tomcat.
  • Automatically overrides any unpacked mytestapp directory in webapps.
  • Tomcat will monitor the war file's modification time; when you replace it, Tomcat will delete the old mytestapp directory, unpack the new war, and reload the app automatically.

3. Verification Steps

  1. Stop Tomcat and delete the webapps/mytestapp directory (if it exists).
  2. Apply either solution above (update server.xml or create the context XML file).
  3. Start Tomcat and verify the app is deployed correctly, using the application.properties from myapp_1.
  4. Replace myapp_1/mytestapp.war with your new version. Wait 10-30 seconds (Tomcat's default monitoring interval) and check if the app reloads automatically with the new code.

内容的提问来源于stack exchange,提问作者Sergey_USB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 12:49:10