dcsimg
Running a command line executable from java
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   David_Hardoon
Posted On:   Wednesday, February 26, 2003 04:23 AM

A pretty strage forward question, I would like to be able to execute
a command line prompt from java if possible. I tried using the
Runtime.getRuntime().exec("echo hello"); - though sadly when I run
the application it did not produce a 'hello' on the prompt.


Another thing is if I have an (for say script) which produces a list
of words, after executing it trough java can I 'grab' the words
thrown out of the script?

Thanks in advance
David.

Re: Running a command line executable from java

Posted By:   Bahman_Barzideh  
Posted On:   Wednesday, February 26, 2003 06:02 AM

Try the following rather crude program:

public class Test{

public static void main (String args []) {

try {
Process p = Runtime.getRuntime ().exec ("echo hello");
java.io.InputStream is = p.getInputStream ();
int res = 0;
while ((res = is.read ()) >= 0) {
System.out.print ((char) res);
}
}
catch (Exception e){
e.printStackTrace ();
}

}
}


It should answer both your questions.
About | Sitemap | Contact