You haven't stated how you are running this command, but I'll presume you are running the command via one of the Runtime class methods, most likely exec(String).
exec(String) will parse your single string using StringTokenizer, which simply splits on whitespace. perl somescript.pl args worked because in this particular case splitting on whitespace does the right thing.
However with your second command, perl -e "print \"Hello World\"", what Perl is getting is not the Perl code print "Hello World", but rather a Perl string containing "print \"Hello World\"". No print command is ever called. Hence nothing is sent to STDOUT.
If you want to pass a Perl snippet, you need instead to use the exec(String[] args) or exec(String[] args, String[] envp) version of exec. Both of these let you pass the individual arguments without making Java guess at how you want to break up a long line into arguments. It would look something like this.
(Note: the code in the readmore tags was too hastily posted and a corrected, more reliable, and tested version is in my reply below. The original code is preserved here so that abramia's initial reply makes sense.)String[] aArgs = { "perl", "-e", "print \"Hello World\"" }; Runtime oRuntime = Runtime.getRuntime(); Process oProcess; try { oProcess = oRuntime.exec(aArgs); } catch (Exception e) { System.out.println("error executing " + aArgs[0]); } OutputStream oOut = oProcess.getOutputStream;
Update: placed code in readme tags, added disclaimer.
In reply to Re: Running perl from java
by ELISHEVA
in thread Running perl from java
by abramia
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |