dcsimg
Problems running an interactive Process
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

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

Re: Problems running an interactive Process

Posted By:   Dan_Winterstein  
Posted On:   Thursday, June 28, 2007 07:30 AM

Posted to Sun Forums: http://forum.java.sun.com/thread.jspa?threadID=5189639
Hopefully someone there can solve this one.

Re: Problems running an interactive Process

Posted By:   Robert_Lybarger  
Posted On:   Thursday, June 14, 2007 01:50 PM

Too many questions and not enough details... Are you sure that output is sent before any input is expected? Why is the sleep present? Are those 'stream gobbler' running in the same execution thread?
About | Sitemap | Contact