简体   繁体   English

ProcessBuilder 进程不提供实时 output

[英]ProcessBuilder process not providing real time output

I'm running ProcessBuilder in a java program using eclipse IDE and Ubuntu 20.04.我正在java程序中运行ProcessBuilder程序,使用Z6B7B655D222222FAA377C512493A093A040Z Z56381F381F381577B.412774B.4125774.2772772774.2772774.2772774.2772772572572572572572572572577B. Specifically for bluetooth LE commands.专门用于蓝牙 LE 命令。 For commands like "hciconfig" the process completes and I can print the output to the console or a textarea (JFX).对于“hciconfig”之类的命令,该过程完成,我可以将 output 打印到控制台或 textarea (JFX)。 Other commands like hcidump run continuously until manually terminated. hcidump 等其他命令会持续运行,直到手动终止。 For these commands I'm unable to print the intermediate output while the process is running.对于这些命令,我无法在进程运行时打印中间 output。 I've tried both the apache.commons CommandLine and the java ProcessBuilder methods probably 6 ways to sundown each.我已经尝试过 apache.commons CommandLine 和 java ProcessBuilder 方法,每种方法可能有 6 种方法来日落。 This should be a fairly straightforward task but so far no luck.这应该是一个相当简单的任务,但到目前为止还没有运气。 Here is the java code:这是 java 代码:

Process pb = new ProcessBuilder()
        .command("hcidump", "-i", "hci0")
        .redirectOutput(ProcessBuilder.Redirect.INHERIT)
        .start();

System.out.println(pb.getOutputStream().toString());

The printed output stream appears to be an address: java.lang.ProcessImpl$ProcessPipeOutputStream@b06aa6f打印的 output stream 似乎是一个地址:java.lang.ProcessImpl$ProcessPipeOutputStream@b06aa6f

How do I access the process output while the process is still running?如何在进程仍在运行时访问进程 output?

The stdout stream of the sub-process is the unfortunately named pb.getInputStream() , not pb.getOutputStream() which is the stdin for the sub-process.子进程的标准输出 stream 不幸命名为pb.getInputStream() ,而不是子进程的标准输入pb.getOutputStream()

Just send stdout to the destination using transferTo, for example:只需使用 transferTo 将 stdout 发送到目的地,例如:

 pb.getInputStream().transferTo(System.out);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM