unixprocess+java+186,Linux上的Java执行进程

I've been struggling for a while now with this problem and i can't seem to fix it.

i already have tried different approaches (Runtime.exec(), ProcessBuiler) but none seem to work.

This is my issue.

I have a laptop which is always on. This laptop runs a java tool connected to an arduino via usb to turn on and off the lights in the house. i have created this program myself, therefore i'm also doing some regular maintenance work on it. Recently i have added a button to restart the program from my html interface (in case i have an update, or if for some other reason i might need to restart the program or i decide to implement auto updating in the near future).

This idea behind this is to start a second instance of the application from the first instance and then System.exit(0) the first instance.

For some reason i'm not able to start a second instance of the application.

Here's some code.

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 = Runtime.getRuntime().exec(startupCommand);

Process proc = builder.command(startupCommand).start();

InputStream stderr = proc.getErrorStream();

InputStreamReader isr = new InputStreamReader(stderr);

BufferedReader br = new BufferedReader(isr);

String line = null;

System.out.println("");

while ((line = br.readLine()) != null) {

System.out.println(line);

}

System.out.println("");

int exitVal = 0;

try {

exitVal = proc.waitFor();

} catch (InterruptedException ex) {

Logger.getLogger(Engine.class.getName()).log(Level.SEVERE, null, ex);

}

System.out.println("Process exitValue: " + exitVal);

} catch (IOException ex) {

ex.printStackTrace();

}

}

System.out.println("Terminating Home Automation");

System.exit(0);

}

java.io.IOException: Cannot run program "java -jar "/Users/NightWalker/Dropbox/Development/Source Code/Java/NightWare Tools/Home Automation/Home Automation Executor/dist/Home_Automation_Executor.jar"": error=2, No such file or directory

at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)

at home.automation.executor.Engine.shutdown(Engine.java:186)

at home.automation.executor.webserver.HTTPGenerator._handleActionCommand(HTTPGenerator.java:190)

at home.automation.executor.webserver.HTTPGenerator._generateHTTPPage(HTTPGenerator.java:165)

at home.automation.executor.webserver.HTTPGenerator.getHTTPPage(HTTPGenerator.java:58)

at home.automation.executor.webserver.HTTPRequestHandler.run(HTTPRequestHandler.java:160)

Caused by: java.io.IOException: error=2, No such file or directory

at java.lang.UNIXProcess.forkAndExec(Native Method)

at java.lang.UNIXProcess.(UNIXProcess.java:53)

at java.lang.ProcessImpl.start(ProcessImpl.java:91)

at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)

... 5 more

解决方案

The problem is this:

String startupCommand = "java -jar \"" + this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath().replace("%20", " ") + "\"";

/* more stuff */ builder.command(startupCommand);

This means Jav will look for a command named java -jar ...stuff with spaces.... But what you want is, that Java looks for a command named java and give that command several parameters.

You should use

/*...*/ builder.command("java", "-jar", jarLocation) /*...*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值