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
    Elisheva,

    Your code snippet is precisely how I am doing it.

    Except for the call to "getOutputStream()" because, according to the javadoc for class Process, method "getInputStream()" obtains data piped from the standard output stream of the process.

    Did you try it out yourself?

    Did it work for you?

    Cheers, Avi.

      The javadoc is right of course and my too quickly typed code is wrong on that point - input vs. output (another item on the list of things I often confuse). However, I promise you, if you've done it right, passing code snippets does work.

      You are using exec(String[]), the one with the array? I'm not at all sure why you are having problems. Are you flushing the output stream before you exit? Throwing away and silencing exceptions? Waiting for the process to finish before querying the output stream? As you know sometimes these problems are in the details or in a small type - an extra quote, a space in the wrong place that the eye doesn't see. Guessing why you are having problems with something that should work is hard without seeing the code.

      Here is a working example of code, that did indeed execute a Perl snippet via the -e option. This was run on Debian Linux (Etch), Perl 5.8.8.

      import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; class RunPerl { public static void main(String[] args) throws IOException { String[] aCmdArgs = { "perl", "-e" , "print \"Hello World, Hello Avi\"" }; Runtime oRuntime = Runtime.getRuntime(); Process oProcess = null; try { oProcess = oRuntime.exec(aCmdArgs); oProcess.waitFor(); } catch (Exception e) { System.out.println("error executing " + aCmdArgs[0]); } /* dump output stream */ BufferedReader is = new BufferedReader ( new InputStreamReader(oProcess.getInputStream())); String sLine; while ((sLine = is.readLine()) != null) { System.out.println(sLine); } System.out.flush(); /* print final result of process */ System.err.println("Exit status=" + oProcess.exitValue()); return; } }

      Update: added demo platform.

        Elisheva, Many thanks for your code. I tried it but I got the same result. If the command array contains two elements, namely "perl" and the name of a perl script, then the code works fine. However, using your example code verbatim, your java code still indicates that no output was produced.

        Thank you for your help.

      Your code snippet is precisely how I am doing it.

      Well then, what is the error message?

        Anon,

        There was no error message.

        As Elisheva correctly mentioned, there simply was no output at all.

        Cheers, Avi.