0

我正在尝试编写一个 Java 程序,它将在 Windows 命令提示符下执行一行代码。我想使用一个外部程序(7-Zip)来提取一些 RAR 文件(它比这更复杂,但对于手头的问题并不重要)。

我得到了运行时实例并使用了 .exec() 方法。当我尝试自己提取一个存档时它工作正常,但是当我尝试同时提取多个存档时,该过程被挂起并且 .waitFor() 永远不会返回。

我研究了这个问题,并认为它是由过程产生的输出引起的。似乎某个缓冲区或另一个缓冲区正在填满并锁定程序。我在这里被概述了。我实现了这个解决方案(除了我使用输入流并将其定向到文件)并且它确实有效。但是,编写所有完全不必要的输出似乎需要花费很多额外的时间。

我想知道是否有办法让 BufferedReader 认为它已经写入了缓冲区中的所有内容而没有实际写入?

感谢您一直阅读到底部!

4

1 回答 1

1

It's not the Java part that is hanging, it is the native application which blocks when it cannot write it's output anymore.

Since you already have the code to make it work in Java, why don't you just stick with that?

The next best option would be to make the external process not produce any output. Maybe there is a "silent" flag? On Linux, you can also pipe to /dev/null, not sure if Windows has an equivalent (I suppose you could pipe into a file).

于 2013-10-03T08:49:56.910 回答