How to start and stop tomcat using java code?

后端 未结 5 590
感情败类
感情败类 2020-12-05 12:19

How to start and stop tomcat using java code?

5条回答
  •  忘掉有多难
    2020-12-05 13:09

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

    By steps:

    Step 1

    Configure the CATALINA_HOME/conf/server.xml as follow:

    
    

    The attribute port is optional. If it is omitted, the default one, 8005, is used.

    The value for shutdown attribute can be anything. This should not be known by others.

    Step 2

    Make the java program send the shutdown command, myShutDownCommand, using java.net.Socket class to 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(); 
    }
    

提交回复
热议问题