title: java block while run shell command using process
body: <p>I have a Java project which help to find all videos information in youtube playlist using youtube-dl. Here is the <code>Main.java</code></p>
  
  <pre><code>import java.io.*;
  
  
  public class Main {
  
  public static void main(String[] args) throws Exception {
      String command1 = "/usr/local/bin/youtube-dl --flat-playlist --dump-single-json https://www.youtube.com/playlist?list=PLFcOH1YaRqUo1yEjY5ly09RFbIpUePF7G";
      String command2 = "/usr/local/bin/youtube-dl --flat-playlist --dump-single-json https://www.youtube.com/playlist?list=PLC6A0625DCA9AAE2D";
      System.out.println(executeCommand(command1));
  
  }
  
  private static String executeCommand(String command) throws IOException, InterruptedException {
      int exitCode = 0;
      String result = "";
      Process process;
      ProcessBuilder builder = new ProcessBuilder(command.replaceAll("[ ]+", " ").split(" "));
      builder.directory(new File("/tmp/test"));
      process = builder.start();
      exitCode = process.waitFor();
      return getStringFromInputStream(process.getInputStream());
  }
  private static String getStringFromInputStream(InputStream is) {
  
      BufferedReader br = null;
      StringBuilder sb = new StringBuilder();
  
      String line;
      try {
  
          br = new BufferedReader(new InputStreamReader(is));
          while ((line = br.readLine()) != null) {
              sb.append(line);
          }
  
      } catch (IOException e) {
          e.printStackTrace();
      } finally {
          if (br != null) {
              try {
                  br.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      return sb.toString();
  }
  }
  </code></pre>
  
  <p>The <code>command1</code> and <code>command2</code> are identical except for the Youbute playlist parameter. The playlist in <code>command2</code> has 409 videos and has 200 videos in <code>command1</code>. I can get result successfully for both command in terminal. Just the <code>command2</code> takes more time, but only  several seconds. But when I run the <code>Main.java</code> (javac Main.java; java Main), for <code>command1</code> it prints result successfully, but for <code>command2</code> it hangs there for several minutes without any result. Here is the <code>jstack</code> for this process</p>
  
  <pre><code>"main" #1 prio=5 os_prio=0 tid=0x00007f828c009800 nid=0xce7 in Object.wait() [0x00007f8293cf7000]
  java.lang.Thread.State: WAITING (on object monitor)
      at java.lang.Object.wait(Native Method)
      - waiting on &lt;0x0000000771df9fe0&gt; (a java.lang.UNIXProcess)
      at java.lang.Object.wait(Object.java:502)
      at java.lang.UNIXProcess.waitFor(UNIXProcess.java:396)
      - locked &lt;0x0000000771df9fe0&gt; (a java.lang.UNIXProcess)
      at Main.executeCommand(Main.java:18)
      at Main.main(Main.java:8)
  </code></pre>
  
  <p>It hangs at <code>exitCode = process.waitFor();</code>. I have no idea about it. Can anyone help me? Many thanks.</p>