0

I'm trying to launch a process in java, read the output, write to the program, then read what it responds with. From all the other answers on SO, this is what I have come up with:

class Main
{
    public static void main(String[] args) {
        String line = "";
        try {
            ProcessBuilder pb = new ProcessBuilder("C:\\myProgram.exe");
            Process p = pb.start();
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }

            output.write("foo");
            output.newLine();
            output.flush();

            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }

            p.destroy();
        }
        catch (IOException e){
        }
    }
}

It launches the program, and gives me the output just as expected.

When i write foo, I expect the program to come back with another response, but it never does.

What am I doing wrong?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jason Macgowan
  • 524
  • 7
  • 15
  • Maybe this could help you ?http://stackoverflow.com/questions/7433073/how-to-write-into-and-read-from-command-line-in-java – Orel Biton Apr 10 '13 at 18:40
  • How many lines are you trying to read? `readline()` will read until it runs out - maybe just call it once? – Evan Knowles Apr 10 '13 at 19:59

0 Answers0