我在Java中有一个交互过程的问题。我有线程来读取STDOUT和STDERR以及一个线程来处理进程的输入。但是,在进程终止之前,STDOUT流中没有可用的数据。然后是整个输出打印一次。
DBG | Pipe action-STDERR started
DBG | Pipe action-STDIN started
DBG | Pipe action-STDOUT started
STDIN | Try to put some input.
STDIN | I cannot see any output.
STDIN | Nevertheless the interaction works.
STDIN | It works on background.
STDIN | Let's terminate the process to see the truth.
STDIN | quit
STDOUT | Enter some text, please: The text is 'Try to put some input.'
STDOUT | Enter some text, please: The text is 'I cannot see any output.'
STDOUT | Enter some text, please: The text is 'Nevertheless the'
STDOUT | Enter some text, please: The text is 'interaction works.'
STDOUT | Enter some text, please: The text is 'It works on background.'
STDOUT | Enter some text, please: The text is 'Let's terminate the process to see the truth.'
STDOUT | Enter some text, please: The text is 'quit'
STDOUT | Bye!
DBG | Trying to kill thread action-STDOUT
DBG | Trying to kill thread action-STDERR
DBG | Trying to kill thread action-STDIN
DBG | Pipe action-STDERR finished
DBG | Finished
DBG | Pipe action-STDIN finished
DBG | Pipe action-STDOUT finished以STDOUT作为前缀的行是由进程写入的行。以STDIN作为前缀的行是我写的行。以DBG为前缀的行是被测试的Java程序写入的行作为调试信息。让我们尝试在系统控制台中执行相同的过程。
Enter some text, please: Text
The text is 'Text'
Enter some text, please: quit
The text is 'quit'
Bye!这种行为满足了我的期望。我被要求输入一些信息。我这样做,并得到答案。
我很惊讶,我发现了几个帖子,包括Stackoverflow,但没有任何答案被标记为可接受的解决方案。 (例如Problem reading InputStream from Java Process。)似乎Java开发人员从来没有处理交互过程的执行。奇怪的是,当进程正在运行时,非交互式进程(如ping)的输出顺序出现。没有任何问题。但是当进程等待用户输入时,输出会以某种方式被阻止。