The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2000



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Need help immediately in getoutputstream

Posted by Aparna on November 27, 2000 at 1:41 PM

Hi :
I have to use an existing application(which takes input from the terminal) written in Java and have to write a wraparound it.The objective of the wraparound is to feed input to the application program and analyze the output of the application.Depending on the application's output i feed the next input to the application.
In other words i have to write an interactive process(co-processes) .
I used the runtime.exec in java which returns a process.I used the process getoutputstream and captured the output from the process(written to the terminal).But my code hangs there.When i try to feed some input, nothing happens?
Do i have to use a seperate thread for io? Can somebody correct my error?
Thanks in advance.
My code follows:

Process p = Runtime.getRuntime().exec(command);
BufferedInputStream buffer =
new BufferedInputStream(p.getInputStream());
BufferedReader commandResult =
new BufferedReader(new InputStreamReader(buffer));

BufferedOutputStream bufferout =
new BufferedOutputStream(p.getOutputStream());
PrintWriter commandInput =
new PrintWriter((new OutputStreamWriter(bufferout)), true);
String s = null;
try {
while (true){
s = commandResult.readLine();
System.out.println("Output: " + s);

commandInput.write("I Like New York");

while ((s = commandResult.readLine()) != null)
System.out.println("Output: " + s);


if (p.exitValue() != 0) {
System.out.println(" -- p.exitValue() != 0");
} catch (Exception e) {
System.out.println("Exception ::" + e);

  • Solution Steve Peterson August 21, 2001 at 6:49 PM (0)

Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us