Click to See Complete Forum and Search --> : Runtime.exec()


August 10th, 1999, 11:10 AM
Hi all,

Could someone please help me with this: I am trying to run a dos/unix command within my java application by using:

String command = "ping hostname";
try {
(Runtime.getRuntime()).exec(command);
}
catch (Exception e) {}

It works fine except for the fact that I don't know how to get the output. For instance, when I issue the command "Ping hostname", it would execute successufully, but I'd like to have the output redirected to a text file: ie:
(Runtime.getRuntime()).exec("ping > result.txt");
I thought that this method should work, but it seems that no output text file is being generated.

Any help on this would be greatly appreciated.

TN

unicman
August 10th, 1999, 11:58 PM
Well Runtime.exec() method returns 'Process' instance. And 'Process' class has getInputStream, getOutputStream etc. methods. Have u tried catching output using 'getInputStream' and then by reading from the stream?

- UnicMan
http://members.tripod.com/unicman

August 11th, 1999, 02:40 PM
Hi unicman,
Thanks for your input, it really works. But now I just wonder if there is a way to capture this output to a string instead of a file (just to avoid applet security manager). Any thought on this will be appreciated.

Thanks in advance
TN

unicman
August 13th, 1999, 12:00 AM
Sorry for the delay in response.

Yes it is possible to capture the output in a string. U get 'InputStream' and attach it to 'DataInputStream' and then u can read line by line if u want or go on appending the string and after all the data has come, start processing it.

I didn't understand why u stored the data in a file.

- UnicMan
http://members.tripod.com/unicman