Ivan
Ivan

Reputation: 529

Runnnig a Java console application from another Java cosole application

I need to run this command from a Java console application:

 /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/

I tried this but it didn't work:

 Process child = Runtime.getRuntime().exec(new String[]{command}); // command is the string written above

I tried many other things, but I didn't get it. Any help?

Btw, I'm using Mac (if it matters).

EDIT: With the code above I get the following error:

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

EDIT #2: I'm now sending this instead of the string "command":

            String[] commands = new String[]{
                    "/bin/bash", "-c",
                    "\"/usr/bin/java -Xms512m -Xmx3072m -jar " + simulatorPath + " "
                    + var1 + " "
                    + var2 + " "
                    + var3 + " "
                    + var4 + " "
                    + var5 + " "
                    + var6 + " "
                    + var7 + " "
                    + var8 + " "
                    + "\""          
            };

But I still get /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() returns number 127.

Upvotes: 0

Views: 515

Answers (1)

ricky ho
ricky ho

Reputation: 73

If command is the above String then

new String[]{command}

will give you a String array with one item. You should try something like

command.split(' ')

where command is a String variable containing your above command.

Upvotes: 2

Related Questions