Java ProcessBuilder:外部进程挂起

发布于 2024-08-09 12:04:55 字数 715 浏览 5 评论 0原文

我正在使用 Java 的 ProcessBuilder 类来运行外部进程。该进程不应在 Java 程序之前终止;它必须在命令/响应模式下保持活动状态。

我知道如果忽视的话,流程流可能很容易“堵塞”,所以我做了以下操作: 该程序在“读取器”线程中读取进程的组合输出和错误流,并使用“写入器”线程来管理命令。读取器线程从进程输出中阻塞字符读取,将它们缓冲到字符串中并分派结果。写入器线程通过 PrintWriter 写入完整的“命令”行;它使用队列来确保两个命令写入不会“太接近”(当前为 100 毫秒),并且在前一个命令的输出完成之前不会写入新命令。我还在每个 println() 之后调用了lush() 和 checkError()。

该方案可以正常工作几秒钟或几分钟,然后读取器线程挂在阻塞的 read() 上。没有错误,没有抛出异常,没有更多的进程输出。此后,什么都不会恢复外部进程(除非重新启动它)。 (顺便说一句,这在 Linux 和 Windows 上都会发生。)

我查看了 Jakarta Commons Exec 和 Plexus Utils http://plexus.codehaus.org/plexus-utils/ 但是(a)都没有给出使用长期进程的示例,并且(b)似乎都没有做任何事情与我所描述的基本不同。

有人知道这里发生了什么事吗? 谢谢!

I'm using Java's ProcessBuilder class to run an external process. The process should not terminate before the Java program does; it must stay alive in command/response mode.

I know that the process streams may easily 'jam' if neglected, so I've done the following:
The program reads the process's combined output and error streams in a "reader" thread, and uses a "writer" thread to manage the commands. The reader thread does blocking character reads from process output, buffers them up into Strings and dispatches the results. The writer thread writes complete "command" lines via a PrintWriter; it uses a queue to ensure that no two command writes are "too close together" (currently 100ms), and that no new command gets written before the output of the previous command is complete. I also call flush() and checkError() after every println().

This scheme works fine for a few seconds or minutes, then the reader thread hangs on the blocking read(). No errors, no exceptions thrown, no more process output. Thereafter nothing will revive the external process (short of restarting it). (BTW this happens on both Linux and Windows.)

I've looked at the code and test-cases in Jakarta Commons Exec and in Plexus Utils http://plexus.codehaus.org/plexus-utils/ but (a) neither gives an example of using a long-lived Process and (b) neither appears to be doing anything basically different from what I've described.

Does anyone have a clue what's happening here please?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海拔太高太耀眼 2024-08-16 12:04:56

只是猜测,但是您是否尝试过取消组合错误和输出流?

Just a guess, but have you tried un-combining the error and output streams?

琉璃繁缕 2024-08-16 12:04:55

你还有一个线程管理stderr吗?您只提到了两个流。

Do you also have a thread managing stderr? You only mention the two streams.

旧街凉风 2024-08-16 12:04:55

我在三个独立的线程中实现了错误、输入和输出流,并且我可以毫无问题地读取和写入外部进程。

我在 Windows/Linux 上使用大量内置应用程序 cmd/bash 以及其他 cmd 行二进制文件进行了测试,它工作正常,除了在某些情况下它只是抛出 io 流异常,我所做的是捕获异常并再次重新启动线程,以便该程序继续运行。

如果您尝试在 Linux 中使用 ssh,那么您可能会遇到无法写入相同标准输入的问题,这是出于安全原因。

尝试从 System.in 获取输入并查看它是否有效,它在我的情况下有效

i had implemented error, input and output stream in three sepereate threads and i can read and write to external processes without any problem.

I tested both on windows/linux with multitude of built in apps cmd/bash as well as other cmd line binaries and it works fine except on some occasions it just throws io stream exception, what i do is catch the exception and restart thread again, so that program keeps on working.

If you are trying to e.g ssh in linux, then you might run across problem like you won't be able to write to same stdin, this is because of security reasons.

Try taking input from System.in and see if it works, it worked in my case

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文