in reply to Running perl from java
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Running perl from java
by abramia (Novice) on Jan 06, 2011 at 12:02 UTC | |
by ELISHEVA (Prior) on Jan 06, 2011 at 13:25 UTC | |
by abramia (Novice) on Jan 09, 2011 at 10:23 UTC | |
by ELISHEVA (Prior) on Jan 09, 2011 at 11:26 UTC | |
by Anonymous Monk on Jan 09, 2011 at 12:13 UTC | |
| |
by abramia (Novice) on Jan 09, 2011 at 12:28 UTC | |
| |
by Anonymous Monk on Jan 06, 2011 at 12:26 UTC | |
by abramia (Novice) on Jan 06, 2011 at 13:03 UTC |