Posted By:
Dan_Winterstein
Posted On:
Thursday, June 14, 2007 12:49 PM
I'm trying to run an interactive Linux process from Java. That is, a program which prompts the user for input as it runs. I want to catch the output and feed in appropriate responses. When I try to run this, it hangs. The process sits there without terminating or emitting any output. NB - I can run non-interactive processes fine. Example code: // Utility class to empty the output streams using read() class StreamGobbler { ... } // Make a process Process process = new ProcessBuilder(MYCOMMAND).start(); // any output? StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream()); // any error messages? StreamGobbler errorGobbler =
More>>
I'm trying to run an interactive Linux process from Java. That is, a program which prompts the user for input as it runs. I want to catch the output and feed in appropriate responses.
When I try to run this, it hangs. The process sits there without terminating or emitting any output.
NB - I can run non-interactive processes fine.
Example code:
// Utility class to empty the output streams using read()
class StreamGobbler { ... }
// Make a process
Process process = new ProcessBuilder(MYCOMMAND).start();
// any output?
StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream());
// any error messages?
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream());
// An input channel
Writer in = new new OutputStreamWriter(process.getOutputStream());
// Wait for some output...
while(true) {
Methods.sleep(500);
String out = outputGobbler.getString();
if (out != "") { // But no output is ever received :(
in.write("my response");
in.flush();
}
}
Thank you for any help! I have trawled the web about this. I found other people who've had the problem, but no solutions.
- Daniel
<<Less