8

I am trying to run a java program from another java program using Runtime.getrunTime().exec

Code :

String java_home = System.getenv("JAVA_HOME");
 String[] command = {""+java_home+"/bin/java -cp -cp /sc/sug/p-lib/*  Tdesigner -cd /pr -in ing.rsp -out /scratch/sug/ng.pla -ad -stopO "};
                try {
                    proc = Runtime.getRuntime().exec(command);
                    proc.waitFor();
                    int exitCode = proc.exitValue();
                } catch (IOException e) {
                        e.printStackTrace();
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }

It gives me following error:

 java.io.IOException: Cannot run program "/net/sl/sc/jdk6/bin/java -cp /sc/sug/p-lib/*  Tdesigner -cd /pr -in ing.rsp -out /scratch/sug/ng.pla -ad -stopOnError ": java.io.IOException: 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)

Can anyone help me to solve the issue. is it that i need to add individual jar files with -cp rather than setting the directory.

3 Answers 3

3

Try removing the extra -cp from the command string array and use individual tokens in the array:

String[] command = { java_home + "/bin/java", "-cp",
   "/sc/sug/p-lib/*", "Tdesigner", "-cd", "/pr", "-in", "ing.rsp",
   "-out", "/scratch/sug/ng.pla", "-ad", "-stopO" };
2

If you use exec(String[]) you must provide the command and its arguments in separate array elements, not all in one as you have done.

2
  • String[] command = {""+java_home+"/bin/java -cp /sc/sug/p-lib/* Tdesigner", "-cd", "/pr", "-in", "ing.rsp", "-out" , "/scratch/sug/ng.pla" ,"-ad", "-stopO"}; Modified the command still get the same error.:( Oct 15, 2012 at 22:12
  • 1
    You still have the java -cp /sc/.../* Tdesigner as all one string, it needs to be separated. Otherwise instead of calling java and passing it the arguments -cp etc., you are trying to run a program called java -cp /sc/... Oct 15, 2012 at 22:17
0

I use scala. Here is my code:

Runtime.getRuntime.exec(Array("/bin/bash", "-c", #your_command)).waitFor()

I think it is easy to understand for others who use java.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.