• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to pass arguments to other programs

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the Intermediate forum...
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic