Search code examples
javaruntimeexternalexecuteruntime.exec

External program blocks when run by Runtime exec


I'm attempting to launch an instance of the VideoLAN program from within a java application. One of the ways I've tried to do this is shown here:

Process p = Runtime.getRuntime().exec("\"C:\\Program Files\\VideoLAN\\VLC\\vlc.exe\" \"http://www.dr.dk/Forms/Published/PlaylistGen.aspx?qid=1316859&odp=true\" :sout=#std{access=udp,mux=ts,dst=127.0.0.1:63928}");

If I execute the above command the vlc program will be launched, and will start a streaming operation (it goes through connect, buffering and then streaming phases).

When the command is executed by Runtime exec (or ProcessBuilder start), the vlc program will hang when it reached the end of the buffering phase. If all threads in the java program are terminated/run to an end, the vlc program will progress to the streaming phase. The java process will not terminate until the vlc process is closed, so this behavior is obviously the result of some sort of coupling between the processes.

Have tried to execute the command indirectly by writing it to a .cmd file and then executing it, but results in the same behavior.

Any ideas for how I can avoid the external process hanging?


Solution

  • Hmm, my guess would be that VLC filled your STDOUT buffer and is hung in a printf statement because STDOUT is waiting for that buffer to empty.

    You need to get the stream for the process's output and read it (even if you discard it).

    I recommend you read this article

    On the 4th page is a good example of how to read the streams in threads so your child process won't block.