title: Calling a shell script from java hangs
body: <p>So I'm trying to execute a shell script which produces a lot of output(in 100s of MBs) from a Java file.
  This hangs the process and never completes.</p>
  
  <p>However, within the shell script, if I redirect the output of the script to some log file or /dev/null Java file executes and completes in a jiffy.</p>
  
  <p>Is it because of amount of data that the Java program never completes?
  If so, is there any documentation as such? or is there any limit on the amount of data(documented)?</p>
  
  <p>Here's how you can simulate this scenario.</p>
  
  <p>Java file will look like:</p>
  
  <pre><code>import java.io.InputStream;
  
  public class LotOfOutput {
  
      public static void main(String[] args) {
          String cmd = "sh a-script-which-outputs-huuggee-data.sh"; 
          try {
              ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
              pb.redirectErrorStream(true);
              Process shell = pb.start();
              InputStream shellIn = shell.getInputStream();
              int shellExitStatus = shell.waitFor();
              System.out.println(shellExitStatus);
              shellIn.close();
          } catch (Exception ignoreMe) {
          }
      }
  }
  </code></pre>
  
  <p>The script 'a-script-which-outputs-huuggee-data.sh' may look like:</p>
  
  <pre><code>#!/bin/sh
  # Toggle the line below
  exec 3&gt;&amp;1 &gt; /dev/null 2&gt;&amp;1
  
  count=1
  while [ $count -le 1000 ]
  do
          cat some-big-file
          ((count++))
  done
  
  echo
  echo Yes I m done
  </code></pre>
  
  <p>Free beer for the right answer. :)</p>