1

I have a perl script that needs 2 arguments to be run. I am currently using Ubuntu and I managed to execute the perl script from terminal by changing directory to where the perl script exists and by writing

perl tool.pl config=../mydefault.config file=../Test

However, when I am trying to run the perl script from my java program (I am using eclipse), It always gives me the message Command Failure. This is the code :

Process process;
try {
    process = Runtime.getRuntime().exec("perl /home/Leen/Desktop/Tools/bin/tool.pl config=../mydefault.config file=../Test");
    process.waitFor();
    if(process.exitValue() == 0) {
        System.out.println("Command Successful");
    } else {
        System.out.println("Command Failure");
    }
} catch(Exception e) {
    System.out.println("Exception: "+ e.toString());
}

So please what is wrong in my code?

2
  • have you checked the exist status of the command when run from the terminal? $?. Are you sure you're running it from the right directory in the Java code? Commented Sep 14, 2012 at 15:28
  • The path inside exec. function seems wrong. Printout System.properties and check the runtime environment before run the script. Or try with absolute path: I mean /home/user/.... Commented Sep 14, 2012 at 15:28

1 Answer 1

6

You should be separating the command from it's arguments in the call to exec(), such as:

Runtime.getRuntime().exec(new String[] {"perl", "/home/Leen...", "config=...", "file=..."});

With what you currently have, Runtime is looked for a command literally named perl /home/Leen/Desktop..., spaces and all.

When you run the whole command from the Terminal, your shell is smart enough to realize that a space delimits the name of the command (perl) from the arguments that should be passed to it (/home/Leen/Desktop..., config=..., file=...). Runtime.exec() is not that smart.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.