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();
        }
    }
}
1
  • 2
    ”i need to restart my tomcat server from java code” - I seriously doubt that. Commented Apr 4, 2020 at 22:59

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

4 Comments

Or something like supervisord.
It is not a homework, in fact i need to restart tomcat in the last step of my script in my project , means after some code, the Tomcat gonna be restarted from Eclipse . i tried the solution in the link but doesn't work.
just updated the answer @bahrinada. Could you take a look at new resources?
yeah i do, in fact i restart tomcat with java code throught the cmd, thanx to @DanielVilas-Boas for the idea ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.