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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |