title: readLine() hangs on last line using Runtime.exec and BufferedStream
body: <p>I am using correct practices mentioned in this article: </p>
  
  <p><a href="http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2" rel="nofollow">http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2</a></p>
  
  <p>I read the Output and Error streams in concurrently. However, when reading the Output stream of the process, it hangs on readLine() after the last line. I have no idea how to work around this. It is the line of the process and it just hangs there.</p>
  
  <p>The command that hangs this is paexec \192.168.1.92 -c -f C:\Windows\ITBBsync0.bat.</p>
  
  <p>Inside the batch file there are several line such as the following: devcon.exe status =USB > C:\Windows\output.txt </p>
  
  <p>When I execute it on command line, process exits with code 0. When I execute it in Java it hangs after reading the last line of output (which is basically the last line of the batch file). I believe the process is not exiting which is why the issue occurs.</p>
  
  <pre><code>import java.io.*;
  class StreamGobbler extends Thread
  {
  InputStream is;
  String type;
  
  StreamGobbler(InputStream is, String type)
  {
      this.is = is;
      this.type = type;
  }
  
  public void run()
  {
      try
      {
          InputStreamReader isr = new InputStreamReader(is);
          BufferedReader br = new BufferedReader(isr);
          String line=null;
          while ((line = br.readLine()) != null){
              System.out.println(type + "&gt;" + line);
              //hangs after reading last line.
          }
          } catch (IOException ioe)
            {
              ioe.printStackTrace();  
            }
  }
  }
  </code></pre>
  
  <p>Also the method that uses the StreamGobbler is the following. There is obviously a parent class which executes this method. </p>
  
  <pre><code>public static boolean cmd(String command){
      try{
      Runtime rt = Runtime.getRuntime();
      System.out.println("Execing " + command);
      Process p = rt.exec(command);
      // any error message?
      StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(),"ERROR");
      StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUTPUT");
  
      errorGobbler.start();
      outputGobbler.start();
  
  
      int exitVal = p.waitFor();
      if (exitVal != 0){
          System.out.println("ExitValue: " + exitVal);        
          System.out.println(command + "EXIT CODE:" + exitVal);
          return false;
      }
      return true;
      }catch(IOException ioe){
          ioe.printStackTrace();
          return false;
      }catch(InterruptedException ie){
          ie.printStackTrace();
          return false;
      }
  }
  </code></pre>
  
  <p>I have visited many threads on StackOverflow and as far as I know I am using best practices for this. Please let me know how I can fix this or why this may be happening.</p>
  
  <p>I can't give you exactly the application I am executing because it is a bit complex, but the application performs some network operations and also executes a batch file which in turns contains more operations.</p>