I am executing an executable with a command line argument using ProcessBuilder
and I am trying to read the output using a BufferdReader
. However, when I print out the input stream of the process, it seems I am first printing out the output, then the input as well.
For example, I am trying to execute "my_command -an-option /path/to/file"
, and when I print out the buffered reader, I am printing out the output followed by the contents of the my file at /path/to/file
. I guess it makes sense that the input stream is reading in my inputp and the output,
public static void d(String file) throws Exception {
ProcessBuilder builder = new ProcessBuilder("my_command", "-an-option", file);
builder.redirectErrorStream(true);
Process process = builder.start();
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
process.waitFor();
String s = null;
while ((s = in.readLine()) != null) System.out.println(s);
in.close();
}
public static void main(String[] args) {
d("/path/to/file");
}
Does anyone know how to make it only print out the output? I want to save the output to a string or something and parse it, etc.