3

I'm using ProcessBuilder to execute a console application that takes one hour to execute.

I want to see output from the console in real time. In my code, the result only appears when the console application is finished.

Here is my code :

List<String> cmd;
ProcessBuilder pb = new ProcessBuilder();
pb.command(cmd);
Process process = pb.start();
process.getOutputStream().close();

InputStream processStdOutput = process.getInputStream();
Reader r = new InputStreamReader(processStdOutput);
BufferedReader br = new BufferedReader(r);
String line;
while ((line = br.readLine()) != null) {
    System.out.println(line); // the output is here
}

int w = process.waitFor();
Ian R. O'Brien
  • 6,682
  • 9
  • 45
  • 73
Hoang Vu Kim
  • 41
  • 1
  • 3

1 Answers1

-1

As pointed out by Hoang, just adding: pb.redirectOutput(ProcessBuilder.Redirect.INHERIT); would do what you need. You can include the stderr by adding pb.redirectErrorStream(true);

Oeg Bizz
  • 104
  • 6