简体   繁体   中英

how to restart tomcat from a running webapp?

I need to restart tomcat service from a webapp running on this tomcat. So I'm trying to execute script that stops tomcat service, and then starts it:

echo "before stop" >> textfile.txt
NET STOP "Tomcat7"

:loop
    timeout 3
    SC query Tomcat7 | FIND "STATE" | FIND "RUNNING" > NUL

IF ERRORLEVEL 1 (
    goto start
) ELSE (
    goto loop
)

:start
    NET START "Tomcat7"

Java code:

   String command = "C:\\Tomcat 7.0\\bin\\restart.bat";
   Process p = Runtime.getRuntime().exec(command);

Tomcat is stopped, but not started. If I run this batch from command line, it works properly.

thank you for your time

What you are asking is not exactly safe and possible but do take a look at Tomcat manager API that allows you to programmatically manipulate Tomcat deployment and instance:

http://tomcat.apache.org/tomcat-7.0-doc/api/index.html

http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/manager/host/HostManagerServlet.html

This worked:

        String fileName = "C:\\Tomcat 7.0\\bin\\restart.bat";
        String[] commands = {"cmd", "/c", "start", "\"DummyTitle\"",fileName};
        Runtime.getRuntime().exec(commands);

taken from http://www.rgagnon.com/javadetails/java-0014.html

Agree with Edmon.

Tomcat is a provider of containers. Each container should act independently of each other, even if they call the services another provides. This should all be done via RMI or alike.

Like Edmon also suggests, you could call using the API, but again... sounds bad. Instead, question why it needs to restart. Then, if there's no work around, use the Tomcat Manager.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM