如何使用JAVA代码执行多个命令?

huangapple go评论98阅读模式
英文:

How can i execute multiple commands with JAVA code?

问题

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("successful");
        } catch (IOException e) {
	        e.printStackTrace();
	    }
    }
}
英文:

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

这可能是好的了解为什么需要从Java代码启动/停止Tomcat,这是一项作业还是其他什么事情吗?在现实世界中,我们有初始化脚本,可以通过在终端中运行它们来启动/停止它。您可以使用一个非常简单的Shell脚本(yourscript.sh)来自动化这个过程,并添加以下内容:

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

如果您真的需要从Java代码中执行此操作,您可能会在以下资源中找到答案:

英文:

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
&lt;path_to_tomcat&gt;/bin/shutdown.sh
&lt;path_to_tomcat&gt;/bin/startup.sh

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

huangapple
  • 本文由 发表于 2020年4月5日 05:05:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/61034772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定