How to start and stop tomcat using java code?

How to start and stop tomcat using java code?

+8
java tomcat
source share
5 answers

You can execute your own commands with java

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

You can send the shutdown command to the shutdown port, both of which can be configured in the root element of the server.xml file of Tomcat.

The steps:

Step 1

Configure CATALINA_HOME / conf / server.xml as follows:

 <Server port="8005" shutdown="myShutDownCommand"> 

The attribute port is optional. If omitted, the default value of 8005 is used.

The shutdown attribute value can be any. This should not be known to others.

Step 2

Make the java program send the shutdown command, myShutDownCommand, using the java.net.Socket class for the shutdown port, 8005.

 try { Socket socket = new Socket("localhost", 8005); if (socket.isConnected()) { PrintWriter pw = new PrintWriter(socket.getOutputStream(), true); pw.println("myShutDownCommand");//send shut down command pw.close(); socket.close(); } } catch (Exception e) { e.printStackTrace(); } 
+17
source share

You need to execute the main method org.apache.catalina.startup.Bootstrap with the parameter "start" .

You will also need the following things:

  • have tomcat/bin/bootstrap.jar in your classpath;
  • -Dcatalina.base to point to $TOMCAT_HOME
  • -Dcatalina.home to point to $TOMCAT_HOME
  • -Djava.io.tmpdir to point to a temporary directory (usually $TOMCAT_HOME/temp )

I also have a set of -noverify options, not sure if it is always needed.

ps it would be nice if you could start accepting answers, your current level is 0/28.

+13
source share

For Linux users using Java, try this:

 Runtime run = Runtime.getRuntime(); Process pr = run.exec("sh startup.sh", null, new File("filePath")); filePathexample = /home/example/apache-tomcat-8.0.47/bin/ 
0
source share

Start (and stop) the built-in Tomcat. Well described how to do this, for example, here .

0
source share

All Articles