Bug using Runtime.getRuntime().exec and Perl
The following program prints four elements:
{A,xB,xC,x}
But I want three elements:
{Ax,Bx,Cx}
The Perl script is supposed to take a string on standard input, append
"x" to the string, and write the new string to standard output.
I'm not sure if the problem is with the Perl script or the Java code,
but since the Perl script is trivial I'm assuming this is a Java issue
and posting to a Java forum.
Setup: PC with Ubuntu Linux 5.10 and JDK 1.5_06
=======================================================
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Vector;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
public class main
{
public static void main(String[] args)
throws Exception
{
String program = "while(<>) {print $_.\"x\";}";
Vector<String> pipe = new Vector<String>();
pipe.add("A");
pipe.add("B");
pipe.add("C");
String[] command = {"perl", "-e",program};
Vector<String> result = new Vector<String>();
Process p = Runtime.getRuntime().exec(command);
OutputStream om = p.getOutputStream();
BufferedWriter w = new BufferedWriter
(new OutputStreamWriter(om));
for(String out:pipe)
{
w.write(out+"\n");
w.flush();
}
om.close();
p.waitFor();
InputStream im = p.getInputStream();
BufferedReader r = new BufferedReader
(new InputStreamReader(im));
String in = r.readLine();
while(in != null)
{
result.add(in);
in = r.readLine();
}
im.close();
System.out.println(result.toString());
}
}