Reputation: 91620
I have some pretty simple Java code in which I'm starting another process and attempting to log all standard out and standard error to SLF4J.
this.service.serverProcess = Runtime.getRuntime().exec(arguments);
this.service.serverIsRunning = true;
BufferedReader serverStdout = new BufferedReader(new InputStreamReader(
this.service.serverProcess.getInputStream()), 16);
BufferedReader serverStderr = new BufferedReader(new InputStreamReader(
this.service.serverProcess.getErrorStream()), 16);
String stdoutLine = null;
String stderrLine = null;
while ((stdoutLine = serverStdout.readLine()) != null ||
((stderrLine = serverStderr.readLine()) != null)) {
if (stdoutLine != null && stdoutLine.trim().length() > 0)
logger.info("{}", stdoutLine);
if (stderrLine != null && stderrLine.trim().length() > 0)
logger.error("{}", stderrLine);
}
try { this.service.serverProcess.waitFor(); } catch (InterruptedException e) {};
this.service.serverIsRunning = false;
Unfortunately, I'm only getting the very first line of output from my process, I never see any more output than that, though I'm sure that there is much more output than what I'm seeing.
What's going wrong here? I'd like it to log just about as soon as the event happens, as the output is important to the logs I'm keeping. How can I get the full output of STDOUT and STDERR of my Process?
Upvotes: 0
Views: 80
Reputation: 1499950
I suspect the problem is that you're never reading from stderr - so if the process has filled up whatever buffer it has for stderr but hasn't printed anything to stdout, you'll be blocking trying to read stdout.
I suggest you start one of the reading loops in a different thread: one reads from stdout and one reads from stderr.
Upvotes: 2