0

我需要从 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。

4

1 回答 1

2

如果命令是上面的字符串,那么

new String[]{command}

将为您提供一个包含一项的字符串数组。你应该尝试类似的东西

command.split(' ')

其中 command 是包含上述命令的字符串变量。

于 2012-05-02T15:36:40.997 回答