我需要从Java控制台应用程序运行此命令:
/usr/bin/java -Xms512m -Xmx3072m -jar /Users/ivan/Desktop/market/market.jar 500 500 1 1 0.1 true true /Users/ivan/Desktop/market/files/simulationResult/
我尝试了这个,但它不起作用:
Process child = Runtime.getRuntime().exec(new String[]{command}); // command is the string written above
我尝试了很多其他的东西,但我没有得到它。有什么帮助吗?
是的,我正在使用Mac(如果重要的话)。编辑:使用上面的代码我收到以下错误:
Exception in thread "main" java.io.IOException: Cannot run program "java -Xms512m -Xmx3072m -jar /Users/ivan/Desktop/market/market.jar 500 500 1 1 0.1 true true /Users/ivan/Desktop/market/files/simulationResult/": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:466)
at SimulationStarter.main(SimulationStarter.java:59)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:53)
at java.lang.ProcessImpl.start(ProcessImpl.java:91)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
... 3 more
编辑#2:我现在发送此而不是字符串“command”:
String[] commands = new String[]{
"/bin/bash", "-c",
"\"/usr/bin/java -Xms512m -Xmx3072m -jar " + simulatorPath + " "
+ var1 + " "
+ var2 + " "
+ var3 + " "
+ var4 + " "
+ var5 + " "
+ var6 + " "
+ var7 + " "
+ var8 + " "
+ "\""
};
但我仍然得到/bin/bash: /usr/bin/java -Xms512m -Xmx3072m -jar /Users/ivan/Desktop/market/market.jar 500 500 2 1 0.1 true true /Users/ivan/Desktop/market : No such file or directory
。
child.waitFor()
返回127号。
答案 0 :(得分:2)
如果command是上面的String那么
new String[]{command}
将为您提供包含一个项目的String数组。你应该尝试像
这样的东西command.split(' ')
其中command是包含上述命令的String变量。