I want to run a console command in my Java application which listens for incoming messages and logs to the console when it receives one. The console command runs fine when I execute it in the terminal. So I want to run the command and then do something when it outputs a line and after that keep on running and listen for other new messages. I tried this through the following code:
try {
ProcessBuilder builder = new ProcessBuilder(PYTHON_PATH, YOWSUP_CLI_PATH, "demos", "-r", "-c", YOWSUP_CONFIG);
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
log.info("Started listening...");
// It does nothing from here on
while ((line = reader.readLine()) != null) {
log.info(line);
}
log.info("Stopped listening.");
} catch (Exception e) {
e.printStackTrace();
}
But when I run this code, it logs the "Started listening..." string, so I send a message to try it out but it doesn't log anything and just keeps on running without doing anything.
If I didn't explain something correctly just say so!
I fixed it using the "-u" parameter in my command, using this answer from MadProgrammer.