Home >Java >javaTutorial >How to Reliably Execute Bash Commands and Capture Their Output in Java?
You aim to create a Java program that executes commands in a bash shell and collects their output. However, you're encountering issues with multiple output streams not working after the first read and the "Broken pipe" exception.
1. Use ProcessBuilder:
Replace Process process = Runtime.getRuntime().exec("/bin/bash"); with:
ProcessBuilder builder = new ProcessBuilder("/bin/bash"); builder.redirectErrorStream(true); Process process = builder.start();
ProcessBuilder allows for redirecting the standard error into the standard output, eliminating the need for separate threads for stderr and stdout.
2. Wait for Output Completion:
The loops reading from the output stream (reader.readline()) will block until the process exits or sends end-of-file. To avoid hangs, ensure that the loops iterate only when the expected output has been received.
3. Use a "Magic Line" to Mark Output End:
To reliably determine when the output from the executed command is complete, consider writing out a specific line (e.g., "--EOF--") to signal the end of output. This technique, demonstrated in the code snippet below, helps handle the scenarios where the process doesn't terminate with a newline character:
while (scan.hasNext()) { String input = scan.nextLine(); if (input.trim().equals("exit")) { writer.write("exit\n"); } else { writer.write("((" + input + ") && echo --EOF--) || echo --EOF--\n"); } writer.flush(); line = reader.readLine(); while (line != null && ! line.trim().equals("--EOF--")) { System.out.println("Stdout: " + line); line = reader.readLine(); } if (line == null) { break; } }
Note: This approach ensures that all output from the executed command is captured before another command is entered. It allows for continuous execution of commands in a threaded scheduled task without encountering the "Broken pipe" exception.
The above is the detailed content of How to Reliably Execute Bash Commands and Capture Their Output in Java?. For more information, please follow other related articles on the PHP Chinese website!