Hi I am trying to execute external program from Java program and read the stdout message in real time
, without waiting for the program to exit. However, i found that there are different stdout behaviour in different .exe program, and I don't know how to handle it.
Example 1:
server1.exe is a console program. When i run it, it will continuously listening on a port. When a client is connected to it, it will generate 1 line of stdout
output every 1 second. It will not exit unless i press "ctrl-C".
In a command prompt, I run this:
server1.exe > stdout.out 2> stderr.err
When client is connected to it, I found that stdout.out file
will be updated in real time. Even though server1.exe is still running, I can open stdout.out file
and read the stdout output in real time.
Example 2:
Similar to server1.exe, server2.exe is also a console program. When i run it, it will also continuously listening on a port. When client is connected to it, it will generate 1 line of stdout output every 1 second. It will not exit unless i press "ctrl-C".
In a command prompt, I run this:
server2.exe > stdout.out 2> stderr.err
Even though client has connected to server2.exe, I found that stdout.out file
is empty. As long as server2.exe is still running, no stdout is written to stdout.out file
. That file is not updated in real time
. When i press ctrl-C
, it suddenly write many lines of output to stdout.out file
.
Assuming that i press ctrl-C
at t=11, it will write all stdout output from t=1 until t=11 into the stdout.out file
. Before this, at t=10, the stdout.out
file is empty.
The program in example 2 is giving me problem because I am unable to read the stdout in real time
in my Java program. My java program is as below:
process = Runtime.getRuntime().exec(command);
input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String inputtext = null;
while ((inputtext = input.readLine()) != null)
{
//print out the text in Real Time, when the .exe program is still running
}
May i know why the program in example 2 will not generate stdout output
unless I press ctrl-C
?
The strange thing is, when i run that program in console window manually, I can see the stdout output printed on the console window every 1 second. But when I try to read it from Java using inputtext = input.readLine()
, inputtext
will be null
as long as the program is still running (I have tested it by printing out inputtext). When I press ctrl-C
, the BufferedReader will suddenly be filled with all the pending stdout output.
How can I read stdout
of server2.exe in real time?