Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-05-2008, 04:41 PM
Member
 
Join Date: Jun 2008
Posts: 1
crookshank is on a distinguished road
Runtime.exec(), handling input and output streams
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ant exec task JavaForums Java Blogs 0 05-20-2008 02:40 PM
Callling stored procedures with input/output parameters and ResultSet Java Tip Java Tips 0 02-11-2008 10:47 AM
Data Streams JavaForums Java Blogs 0 12-26-2007 04:01 PM
Exec cmd from Java application javaplus New To Java 0 12-17-2007 10:41 AM
how to take input and verify input in Java programs bilal_ali_java Advanced Java 0 07-21-2007 10:46 AM


All times are GMT +3. The time now is 12:40 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org