我正在尝试从java代码执行一个外部命令,但是我注意到Runtime.getRuntime().exec(...)和之间存在差异new ProcessBuilder(...).start().
使用时Runtime:
Process p = Runtime.getRuntime().exec(installation_path +
uninstall_path +
uninstall_command +
uninstall_arguments);
p.waitFor();
Run Code Online (Sandbox Code Playgroud)
exitValue为0,命令终止ok.
但是,有ProcessBuilder:
Process p = (new ProcessBuilder(installation_path +
uninstall_path +
uninstall_command,
uninstall_arguments)).start();
p.waitFor();
Run Code Online (Sandbox Code Playgroud)
退出值为1001,命令终止于中间,但waitFor返回.
我该怎么做才能解决问题ProcessBuilder?
我一直在努力解决这个问题,我似乎无法修复它.我已经尝试过不同的方法(Runtime.exec(),ProcessBuiler),但似乎都没有.
这是我的问题.我有一台永远在线的笔记本电脑.这台笔记本电脑运行一个java工具连接到arduino通过USB打开和关闭房子里的灯.我自己创建了这个程序,因此我也在做一些定期的维护工作.最近我添加了一个按钮,从我的html界面重启程序(如果我有更新,或者由于某些其他原因我可能需要重新启动程序或我决定在不久的将来实现自动更新).
这背后的想法是从第一个实例启动应用程序的第二个实例,然后从System.exit(0)启动第一个实例.
由于某种原因,我无法启动应用程序的第二个实例.这是一些代码.
public void shutdown(boolean restart) {
if (this.serial != null) {
this.serial.disconnect();
}
if (restart) {
System.out.println(this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
String startupCommand = "java -jar \"" + this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath().replace("%20", " ") + "\"";
ProcessBuilder builder = new ProcessBuilder();
// String[] command = new String[1];
// command[0] = "-jar \"" + (System.getProperty("user.dir") + "/Home_Automation_Executor.jar") + "\"";
try {
// //System.out.println("Restarting Home Automation with command: " + command[0]);
// System.out.println("Restarting Home Automation with command: " + startupCommand);
// Runtime.getRuntime().exec("bash");
// Process proc = …Run Code Online (Sandbox Code Playgroud)