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?
You should be separating the command from it’s arguments in the call to
exec(), such as: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.