title: A Java program that runs an external "java myprog < input.txt > output.txt"
body: <p>I want to write a Java program that runs an external "java myprog &lt; input.txt > output.txt" command. The eventual goal is to run this command on two different programs and compare their output similarity from their respective output files.</p>
  
  <p>I think I've read just about every relevant article about using ProcessBuilder to run an external program, and the few entries about handling user input in that external program, but I still can't get things working. From what I have read, I think the best approach is to not run the exact command above, but instead read the input.txt file and feed it byte-by-byte into the Process object, then collect the output and write it to output.txt ... I am 100% open to other options. </p>
  
  <p>I put together the code below based on my readings. It seems to correctly feed input from input.txt into myprog, but when I try to print the external program's output to the console to verify, the program hangs at the point where (surprise) user input is expected in myprog.</p>
  
  <p>I get the same issues with and without the redirectErrorStream(true) line.</p>
  
  <p>I really want this to be in Java since I plan to share the source code with the people whose program outputs I will compare, and they are primarily only familiar with Java.</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  
  public class test7 {
  
      public static void main(String args[]) {
  
          try {
              // WANT: "java myprog &lt; input.txt &gt; output.txt"
              String inputFile = "input.txt";
              String outputFile = "output.txt";
  
              ProcessBuilder pb = new ProcessBuilder("java","myprog");
              pb.redirectErrorStream(true); // merge stdout, stderr of process
              Process p = pb.start();
  
              // write input to the running program
              OutputStream pos = p.getOutputStream();
              InputStream fis = new FileInputStream(inputFile);
              int read = 0;
              while ( (read = fis.read()) != -1) {
                  pos.write(read);
              }
              fis.close();
  
              // get output of running program
              InputStreamReader isr = new  InputStreamReader(p.getInputStream());
              BufferedReader br = new BufferedReader(isr);
  
              // HANGS HERE WHEN USER INPUT REQUIRED
              String lineRead;
              while ((lineRead = br.readLine()) != null) {
                  System.out.println(lineRead);
              }
  
          }
          catch (IOException e) {
              e.printStackTrace(); 
          }
      } // end main
  
  }
  </code></pre>
  
  <p>Here is the content of myprog.java:</p>
  
  <pre><code>import java.io.*;
  
  public class myprog {
  
      public static void main(String args[]) throws IOException {
  
          System.out.println("Hello world!");
          System.out.println("Enter something:");
  
          BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
  
          // the readLine() command causes ProcessBuilder to hang
          cin.readLine();
      }   
  }
  </code></pre>
  
  <p>And the input.txt file is just</p>
  
  <pre><code>p
  </code></pre>
  
  <p>The output.txt file should be</p>
  
  <pre><code>Hello world!
  Enter something:
  </code></pre>