之前我就是没有写waitFor() 方法,所以进程启动之后就结束了
public static void executeCmd(String command) throws IOException {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("cmd /c " + command);
try {
process.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
System.err.println("准备运行 prometheus...");
String url = "D:\\software\\prometheus-2.14.0.windows-amd64\\start.bat";
executeCmd(url);
System.err.println("成功启动 prometheus...");
} catch (IOException e) {
System.out.println("Error exec!");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
start.bat与exe文件在同目录
cd /d %~dp0
start /B prometheus.exe 1>nul 2>nul