我在 java 文件中使用终端命令“java -jar secondApp.jar”来启动 secondApp.jar。
即使第一个应用程序被杀死,我也需要 secondApp.jar 来运行。
此场景在 Windows 环境中完美运行。但是当我在 linux 环境(Ubuntu 16.04)中测试它时,似乎杀死第一个进程会杀死两个进程。
这是我用来启动第二个应用程序的代码。
String command = "java -jar secondApp.jar"
Process process = Runtime.getRuntime().exec(command);
我做错了什么?
最佳答案
使用所需的 java 命令准备一个批处理文件和一个 linux 脚本文件,然后试试这个:
if (SystemUtils.IS_OS_WINDOWS) {
// run batch file
String batchFullPath = new File("C:\\myBatchFile.bat").getAbsolutePath();
Runtime.getRuntime().exec("cmd /C start " + batchFullPath);
} else if (SystemUtils.IS_OS_LINUX) {
// run linux script
String scriptFullPath = new File("~/myScriptFile.sh").getAbsolutePath();
File workingDir = new File("~");
Runtime.getRuntime().exec("/usr/bin/xterm " + scriptFullPath, null, workingDir);
} else {
throw new RuntimeException("Unsupported Operating System");
}
(使用 xterm,因为假设每台 Linux 机器都安装了它是相当安全的)
关于java - 在 java 程序中启动另一个 java 程序并终止母进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49686403/