0

I need to restart my Tomcat server from Java code. I'm a beginner in Java. I try to do that by cmd. I need to stop tomcat then restart it. I try this code. It works with just two commands (one &&) and it doesn't work if I add a third command (two && in the line exec("cmd /c start cmd.exe ...)).

PS: If another way exists to restart Tomcat with Java code please tell me

public class restart_tomcat {

    public static void main(String[] args) throws SDKException, IOException {
        Runtime rt =  Runtime.getRuntime();

        try {
            // Process process1 = Runtime.getRuntime().exec("cmd /c start cmd.exe /K " + cmd1);
            rt.exec("cmd /c start cmd.exe /K \"cd C:\\\\Program Files (x86)\\\\SAP BusinessObjects\\\\tomcat\\\\bin&&startup.bat\"");
            System.out.println("succesful");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
charles-allen
  • 3,891
  • 2
  • 23
  • 35

1 Answers1

2

It would be good to know why do you need to start/stop Tomcat from java code, is that a homework or something else? In the real-world, we have initialization scripts that can start/stop it by simply running them from the terminal. You can automate that with a very simple shell script (yourscript.sh) and add the following content:

#!/bin/bash
<path_to_tomcat>/bin/shutdown.sh
<path_to_tomcat>/bin/startup.sh

If you really need to do it from Java code, you may find your answer in one of these resources: