From Java, I run a C++ app which logs its progress as it proceeds, and want to monitor that there is some progress continually, so (in Java) I monitor process.getInputStream() and process.getErrorStream(). When I run the C++ app from the command line, the log appears one line at a time as it progresses (about 1 line very 1-2 seconds). From the Java monitor, I’m not getting the data in realtime, and am not sure who is buffering it.
I can look at the C++ code (though it’s rather complex) and try to put flush() calls as necessary, but the data appears in realtime when I run it from cmd.exe, so before I do that, I’d like to check out if there is something else along the way that I have to do. Is there something in Java I have to do to get the data in realtime, to make sure the input stream is not buffered?
I’m running in Windows XP and above.
I tried
for (;;) {
int c = is.read();
if (c < 0) {
break;
}
System.out.print(c);
System.out.flush();
}
if (true) {
return;
}
and also tried
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
synchronized (lastActivityTime) {
lastActivityTime = java.util.Calendar.getInstance().getTimeInMillis();
}
if (pw != null) {
pw.println(type + line);
pw.flush();
}
}
It is the source process that is doing the buffering, in the
stdiolibrary. There is nothing you can do about that at the Java end. You have to putfflush()calls into the C++ code, or put up with it and wait. The data appears in real time when you run from cmd.exe because stdio behaves differently when attached to what it thinks is a real terminal.