简体   繁体   中英

ProcessBuilder process not providing real time output

I'm running ProcessBuilder in a java program using eclipse IDE and Ubuntu 20.04. Specifically for bluetooth LE commands. For commands like "hciconfig" the process completes and I can print the output to the console or a textarea (JFX). Other commands like hcidump run continuously until manually terminated. For these commands I'm unable to print the intermediate output while the process is running. I've tried both the apache.commons CommandLine and the java ProcessBuilder methods probably 6 ways to sundown each. This should be a fairly straightforward task but so far no luck. Here is the java code:

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

How do I access the process output while the process is still running?

The stdout stream of the sub-process is the unfortunately named pb.getInputStream() , not pb.getOutputStream() which is the stdin for the sub-process.

Just send stdout to the destination using transferTo, for example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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