由Runtime exec运行时外部程序块 [英] External program blocks when run by Runtime exec

查看:150
本文介绍了由Runtime exec运行时外部程序块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Java应用程序中启动VideoLAN程序的实例.此处显示了我尝试执行此操作的方法之一:

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}");

如果我执行上述命令,则vlc程序将启动,并将启动流操作(它先经过连接,缓冲然后流化阶段).

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).

当命令由Runtime exec执行(或ProcessBuilder启动)时,vlc程序在到达缓冲阶段结束时将挂起.如果java程序中的所有线程都终止/运行到最后,则vlc程序将进入流传输阶段.在vlc进程关闭之前,java进程不会终止,因此,此行为显然是进程之间进行某种耦合的结果.

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.

尝试通过将命令写入.cmd文件然后执行来间接执行该命令,但是会导致相同的行为.

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?

推荐答案

嗯,我猜是VLC填充了您的STDOUT缓冲区并挂在printf语句中,因为STDOUT正在等待该缓冲区为空.

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).

我建议您阅读文章

第4页上的示例很好地说明了如何读取线程中的流,以使子进程不会阻塞.

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

这篇关于由Runtime exec运行时外部程序块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆