Question

I am trying to call a perl script from java runtime. It worked fine on my windows7 laptop with the following code,

try {
    String cmdString= "c:\\perl64\\bin\\perl.exe c:\\perl64\\eg\\userinput.pl \""+arg1+"\" \""+arg2+"\"";
    Process p = Runtime.getRuntime().exec(cmdString);
} catch(IOException e) {
    System.out.println(e);
}

The perl script runs and produces what I expect (update database).

When I move the whole thing over to a remote CentOS server, it doesn't work anymore. The script is the same and the java code is,

try {
    String cmdString= "/opt/lampp/bin/perl /home/support/scripts/userinput.pl \""+arg1+"\" \""+arg2+"\" > /tmp/userinput.log";
    log(cmdString);
    Process p = Runtime.getRuntime().exec(cmdString);
} catch(IOException e) {
    System.out.println(e);
}

I added redirect to /tmp/userinput.log after I see the script is not working. But there is no log file created at all. I also added log to make sure this part of the java code did get executed, and indeed it did. I also tried to add "/bin/bash " in front of the comString and it didn't make a difference. However, when I run the cmdString directly on the remote server from command line, it works without problem.

Now, when I changed the cmdString to "touch /tmp/userinput.log", it does create the empty log file.

So I know the Runtime.getRuntime().exec(cmdString) command ran, and the cmdString works when entered on command line, and a simple "touch" command would work with this setup. But I am totally lost why the actual cmdString that calls the perl script doesn't work, and there is no message whatsoever to tell me what is wrong.

Can someone please help?

Was it helpful?

Solution

Frist, separate each parameter for the command and use the version of exec which takes a String[] (you won't have to worry about quoting issues). also, shell redirection won't work since java isn't executing a shell.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top