英文:
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代码中执行此操作,您可能会在以下资源中找到答案:
- https://stackoverflow.com/questions/21321897/start-and-stop-tomcat-from-java-code
- https://stackoverflow.com/questions/48109709/restart-tomcat-with-java
- https://stackoverflow.com/questions/21321897/start-and-stop-tomcat-from-java-code
- https://stackoverflow.com/questions/24929175/how-to-start-tomcat-server-programmatically-in-java
- https://stackoverflow.com/questions/11874789/how-to-restart-tomcat-from-a-running-webapp
英文:
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:
- https://stackoverflow.com/questions/21321897/start-and-stop-tomcat-from-java-code
- https://stackoverflow.com/questions/48109709/restart-tomcat-with-java
- https://stackoverflow.com/questions/21321897/start-and-stop-tomcat-from-java-code
- https://stackoverflow.com/questions/24929175/how-to-start-tomcat-server-programmatically-in-java
- https://stackoverflow.com/questions/11874789/how-to-restart-tomcat-from-a-running-webapp
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论