由运行时执行程序运行时外部程序块
我正在尝试从 java 应用程序中启动 VideoLAN 程序的实例。我尝试执行此操作的方法之一如下所示:
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 程序将启动,并将启动流操作(它经历连接、缓冲和流阶段)。
当该命令由 Runtime exec(或 ProcessBuilder start)执行时,vlc 程序将在到达缓冲阶段结束时挂起。如果java程序中的所有线程都终止/运行结束,则vlc程序将进入流阶段。在vlc进程关闭之前,java进程不会终止,因此这种行为显然是进程之间某种耦合的结果。
尝试通过将命令写入 .cmd 文件然后执行来间接执行该命令,但会导致相同的行为。
关于如何避免外部进程挂起的任何想法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,我的猜测是 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).
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.
这个网站太棒了:)。由于某种原因,我认为已经尝试过的一种方法突然开始起作用。
问题是 vlc 写入其 stdErrOut(在提示符下执行时不可见)。一旦某些输出缓冲区已满,它就会阻塞。解决方案是将 stdErr 重定向到 stdOut,然后让线程清空进程对象的输入流。
然而,这不是一个最佳解决方案,因为我需要大量的外部进程,并且您无法在其输入流上执行非阻塞 I/O。将尝试使用计时器服务驱动多个进程的空读取。关于如何解耦流程以避免此问题的其他建议非常受欢迎。
This site is fantastic :). For some reason an approach I thought had already been tried suddenly started working.
The problem is that vlc writes to its stdErrOut (which is not visible when executed in a prompt). It then blocks once some output buffer is full. A solution is to have a stdErr redirected to stdOut and then have a thread empty the input stream of the process object.
It is however not an optimal solution, since I need a fair amount of external processes, and you can't do non-blocking I/O on their input streams. Will experiment a bit with having a timer service drive empty-reading for a number of processes. Other suggestions for how to de-couple the processes to avoid this problem are very welcome.