• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to pass arguments to other programs  RSS feed

 
Karan Raj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

As part of my application i have to interact with other program..whose source code is not available to me. I am trying to excute that program using "Runtime" class.

name of the other program is rimer
-----------------------------------------------
#$primer -c
#$primer program running...
#$Enter the temp : 30
#$wrong entry...temp should be between 40-70.reenter
#$Enter the temp : 45
#$Enter the wordlength:20

----------------------------------------------
above is a sample program i have. i have to integrate it with my java application. i am able to run the program using "Runtime"..but how can i send the arguments & how can i handle error situations..?

please help me in this

thanks in advance
karan
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Intermediate forum...
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

When you launch a process using Runtime.exec(), you get back a Process object. The Process class has a method getInputStream() that returns an InputStream from which you can read the program's output. It also has a method getOutputStream() that returns... well, you guessed it, an OutputStream. If you write data to this OutputStream, it appears at the program's standard input.

So between these two methods (and a getErrorStream() method, too, for standard error) you can emulate a user typing at a command line.
 
Karan Raj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i tried to run the program with following sample code.but i am not able to pass command line inputs.

sample code:
---------------------------
public static void main(String args[]){

Runtime rr=Runtime.getRuntime();
try{

Process p=rr.exec("primer");BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));

BufferedWriter or = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
String l=new String();


while((l=br.readLine())!=null){


System.out.println(">>>"+l);


try{

or.write(4);
or.write('\n');
or.write('\r');

or.flush();

}catch(Exception e){
System.out.println("exception"+e);
}

}
System.out.flush();

}catch(Exception ioe){
System.out.println("Exception is:"+ioe);
}


}

}
---------------------------------
if i run the above sample code i am not able to see anything until i terminate the program.

please help me in this..if you have any sample code..that will help me in understanding how to do processing.

thanks in advance
karan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!