简体   繁体   中英

How to start Tomcat Server programmatically in Java

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

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.

If you use maven give a look to it's cargo plugin: http://cargo.codehaus.org/Tomcat+7.x I'm sure other build/dependency management tools have something similar as well. ( http://cargo.codehaus.org/Functional+testing ) Regards, Tamás

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