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.


In reply to Re^3: Running perl from java by ELISHEVA
in thread Running perl from java by abramia

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.