title: Java - Use exec and read output while program is still running
body: <p>So I'm creating a gui for an exe in Java. Bit cumbersome but I'm trying. Due to this program being a console program, I need to read the output of the program while it's running and put that into a Swing textbox. But every way I've tried it is either unstable or simply does not work at all. Could anyone help me out with this? Note I have tried Apache Commons exec with little success. Also I'm trying to make this cross-platform and have created a method for getting the OS:</p>
  
  <pre><code>public enum OSType {
      WINDOWS, OSX, LINUX, DAFUQ
  }
  
  /**
   * Gets OS running on then caches result
   * @return OS running on
   **/
  public static OSType getOS() {
      if (os != null)
          return os;
      else {
          os = getOperatingSystem();
          return os;
      }
  }
  
  /** Don't use this! Use {@link Frame#getOS()} instead **/
  public static OSType getOperatingSystem() {
      String OS = System.getProperty("os.name").toLowerCase();
      if (OS.indexOf("win") &gt;= 0)
          return OSType.WINDOWS;
      else if (OS.indexOf("mac") &gt;= 0)
          return OSType.OSX;
      else if (OS.indexOf("nix") &gt;= 0 || OS.indexOf("nux") &gt;= 0 || OS.indexOf("aix") &gt; 0)
          return OSType.LINUX;
      else
          return OSType.DAFUQ;
  }
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>To clarify, I am creating a .jar program that acts as a gui for a EXE. This is the code that I am currently trying to use to start the program:</p>
  
  <pre><code>ProcessBuilder prc = new ProcessBuilder(commands);
  
          prc.redirectErrorStream(true);
  
          try {
              Frame.exec = prc.start();
          } catch (IOException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }
  </code></pre>
  
  <p>The <code>commands</code> variable is just a list for the commands I need to execute. The program is running correctly because I can see it working on the task manager. Here is also the code for how I am reading from the InputStream of the process: </p>
  
  <pre><code>BufferedReader in = new BufferedReader(new InputStreamReader(exec.getInputStream()));
          String line;
          while ((line = in.readLine()) != null) {
              System.out.println(line);
          }
  </code></pre>
  
  <p>I am trying to read from the stream asynchronously, so this code is in another thread, which is why I needed to store the "exec" variable.</p>