(Process) Problems with reading/writing to an external Process
After starting an external subprocess in java we turn to have a problem with reading and
writing to the subprocess inputstream and outputstream if the external procress is an
interactive process that is if an input is needed to be written to the OutputStream of the
process.
when an external subprocess is started and needs no input(writing to the OutputStream of the
subprocess), the Inputstream of the subprocess can be read but if the subprocess is
interactive where the user has to select from a list and write back to the outputstream of
the process no result is displayed that is the inputstream of the process blocks. if the
outputstream of the process is closed, one can then read the inputstream but it then runs
infinitely.(code below)
can someone explain to me how i can read the inputstream and write to the outputstream of an
external process without running into the above problems
..........................
publicclass StreamConsumerextends Thread{
private InputStream is;
public StreamConsumer (InputStream is{
this.is = is;
}
publicvoid run(){
InputStreamReader reader =newInputStreamReader (is);
BufferedReader buffer =new BufferedReader(reader);
String line;
try{
while ((line = buffer.readLine()) !=null){
System.out.println(line);
}
}catch (Exception e){
// ignore errors
}
}
}
.....
/* in main*/
/*
case one
if the external process is not an interact process no problem occurs we have an output
*/
Process p = Runtime.exec(command);
StreamConsumer sc1=new StreamConsumer(p.getErrorStream() )
StreamConsumer sc2 =new StreamConsumer(p.getInputStream() )
sc1.start();
sc2.start();
/*
case two
if the external process is an interact process no results are displayed that is a list for
the user to choose
*/
Process p = Runtime.exec(command);
StreamConsumer sc1=new StreamConsumer(p.getErrorStream() )
StreamConsumer sc2 =new StreamConsumer(p.getInputStream() )
sc1.start();
sc2.start();
.......no results
/*
case three
if the outputstream of an external problem is closed an endless looping occurs. the
inputstream never reaches the end. and this is not what we want we down want to close the
outputstream of the process rather we expect the inputstream to display a selection list for
the user to choose.
*/
Process p = Runtime.exec(command);
StreamConsumer sc1=new StreamConsumer(p.getErrorStream() )
StreamConsumer sc2 =new StreamConsumer(p.getInputStream() )
sc1.start();
sc2.start();
p.getOutputStream().close();
***endless looping of the inputstream occurs

