The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2000

Advertisement

Advertisement

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.

Message:

Solution

Posted by Steve Peterson on August 21, 2001 at 6:49 PM

I'm sure you have figured it out by now. I can across your posting in an attempt to solve the same problem. You were correct that a separate thread was needed.

I hacked up your example and used the Unix cat command, which will block on stdin until closed:

import java.io.*;

public class StdOutTest {

public static void main( String[] args ) {

Process p;
try {
p = Runtime.getRuntime().exec("cat"); // as in Unix command "cat"
} catch( java.io.IOException e ) {
System.err.println(e);
return;
}

new OutputStuffThread(p).start();

BufferedInputStream buffer = new BufferedInputStream(p.getInputStream());
BufferedReader commandResult = new BufferedReader(new InputStreamReader(buffer));

try {
while (true){
String s = commandResult.readLine();
if(s == null )
break;
System.out.println("Output: " + s);
System.out.flush();
}

commandResult.close();

} catch (java.io.IOException e) {
System.out.println("Exception ::" + e);
}
}

}

class OutputStuffThread extends Thread {
private Process m_process;

public OutputStuffThread( Process process) {
m_process = process;
}

public void run() {
try {
BufferedOutputStream bufferout = new BufferedOutputStream(m_process.getOutputStream());
PrintWriter commandInput = new PrintWriter((new OutputStreamWriter(bufferout)), true);
commandInput.println("I Like New York");
commandInput.println("exit");
commandInput.close();
} catch( Exception e ) {
System.err.println(e);
}
}

}


> 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.
> Aparna
> 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();
> if(s.equals("Dattapuram"))
> break;
> System.out.println("Output: " + s);
> System.out.flush();
> }

> commandInput.write("I Like New York");
> commandInput.write("exit");
>
> while ((s = commandResult.readLine()) != null)
> System.out.println("Output: " + s);

> commandInput.close();
> commandResult.close();

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






Replies:

Sponsored Links



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