processbuilder - java block while run shell command using process -


i have java project find videos information in youtube playlist using youtube-dl. here main.java

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();     } {         if (br != null) {             try {                 br.close();             } catch (ioexception e) {                 e.printstacktrace();             }         }     }     return sb.tostring(); } } 

the command1 , command2 identical except youbute playlist parameter. playlist in command2 has 409 videos , has 200 videos in command1. can result both command in terminal. command2 takes more time, several seconds. when run main.java (javac main.java; java main), command1 prints result successfully, command2 hangs there several minutes without result. here jstack process

"main" #1 prio=5 os_prio=0 tid=0x00007f828c009800 nid=0xce7 in object.wait() [0x00007f8293cf7000] java.lang.thread.state: waiting (on object monitor)     @ java.lang.object.wait(native method)     - waiting on <0x0000000771df9fe0> (a java.lang.unixprocess)     @ java.lang.object.wait(object.java:502)     @ java.lang.unixprocess.waitfor(unixprocess.java:396)     - locked <0x0000000771df9fe0> (a java.lang.unixprocess)     @ main.executecommand(main.java:18)     @ main.main(main.java:8) 

it hangs @ exitcode = process.waitfor();. have no idea it. can me? many thanks.

as mentioned in comments, default output of subprocess sent pipe can read using process.getinputstream(). if subprocess generates lots of output , java program doesn't consume it, pipe's buffer fill , subprocess block on writing.

the easiest solution call .inheritio() on processbuilder. send output console instead of buffering in memory (same input , error streams).


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -