简体   繁体   中英

How to start Tomcat Server using Java Code?

I've gone through some of the links for this on Google. I'm also able to shut down the server through Java code but can't seem to understand how the Java code to run the server will execute when the server is shutdown. Any explanations?

You can execute native commands on startup using java:

  String command = "c:\program files\tomcat\bin\startup.bat";//for linux use .sh
  Process child = Runtime.getRuntime().exec(command);

In shutdown use:

 String command = "c:\program files\tomcat\bin/shutdown..bat";//for linux use .sh
  Process child = Runtime.getRuntime().exec(command);

This answer is more appealing <-- Click

System.getProperty("catalina.home")

This is more promising and suggestive way of triggaring such a batch call

What you are basically trying to do is have a Tomcat bootstrap itself (impossible because this is a chicken or the egg problem) or shut itself down (possible).

I suggest you either run two instances of Tomcat on different ports or run a different component that is able to expose services via http (Apache webserver with cgi, Spring Boot based webapp, ...) One instance will solely be used for the management part and runs on a different port (or vhost if Apache webserver is involved)

The management web interface then calls the standard startup and shutdown command files provided for both Linux and Windows and either keeps track of the state or searches for the Tomcat process using ps or tasklist .

In case you want to start and stop applications deployed to Tomcat and not the complete Tomcat instance you can use Tomcat Manager instead.

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