hello friends,

I am executing a command line program using exec. Since it is interactive i need to handle both input and output streams. So i used thread to read output of process. I need to display the output in Jtextarea. When the process asks to press enter key, i press the next button.

class mainClass {
...
...
...
...
...
...
..

mainClass() {
try {
String[] cmd1 = { "java", "helloworld"};
ddgen = r.exec(cmd1);
ISErr= ddgen.getErrorStream();
readStream(ISErr, "main error");
ISIn= ddgen.getInputStream();
readStream(ISIn, "main input");
BufOut = new BufferedOutputStream (ddgen.getOutputStream());
int exitVal = ddgen.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Exception e) {
e.printStackTrace();
}
}

/************** Reading Native's Output************************/
void readStream(InputStream ins, String offset) throws IOException
{
final InputStream in= ins ;

T=new Thread("reader")
{
public void run()
{
try{
int ptr = 0;
byte[] buffer ;

while (true)
{
int x=in.available();
if (x<=0) continue ;
buffer = new byte[x];
ptr = in.read(buffer,0,x);
resultTxt.append("Process :"+ new String(buffer ));
}
}catch (Exception ex){
ex.printStackTrace( );
}
}
};
T.start();
}

/************** Writing to Native ************************/
synchronized void writeToStream(BufferedOutputStream out ) throws IOException
{
if ( ddgen==null || out==null){
resultTxt.append("\n"+"No Process!! :"+"\n");
return;
}

out.write("\n".getBytes());
out.flush();
notifyAll();
}

public void actionPerformed(ActionEvent ae)
{

if(ae.getActionCommand().equals("next"))
{
try{
writeToStream(BufOut);
}catch (Exception ex){
resultTxt.append("\n"+"ERROR :"+ex.getMessage() +"\n");
ex.printStackTrace( );
}//end exceptionhandling
}
}

}

Currently the output of the above program is it prints the output till first enter after that i press enter many times and then execution of process takes place and full output is displayed.

Kindly help.
Thanx in advance.