How to start Tomcat Server programmatically in Java

前端 未结 2 994
忘了有多久
忘了有多久 2020-12-22 08:59

I want to start tomcat server programmatically in JAVA. Please help me

2条回答
  •  囚心锁ツ
    2020-12-22 09:24

    If I understood you well, you are interested in running tomcat on your event from application. If it is the case, you can write your own method to run tomcat.

    This is example:

    public void stopRunTomcat(){
        try{
            Socket s = new Socket(server,8005);
            if(s.isConnected()){
                PrintWriter print = new PrintWriter(s.getOutputStream(),true);
                //Stop tomcat if it is already started
                print.println("SHUTDOWN"); 
                print.close();
                s.close();
            }
            //Run tomcat 
            Runtime.getRuntime().exec(System.getProperty("catalina.home")+"\\bin\\startup.sh");
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
    

    You have to adopt this code to your paths and OS. After that you can call this method from event which have to raise tomcat.

    I hope that helps.

提交回复
热议问题