One Two Three
One Two Three

Reputation: 23497

display the output-stream of a Process returned by Runtime.exec()

How do I print to stdout the output string obtained executing a command?

So Runtime.getRuntime().exec() would return a Process, and by calling getOutputStream(), I can obtain the object as follows, but how do I display the content of it to stdout?

OutputStream out = Runtime.getRuntime().exec("ls").getOutputStream();

Thanks

Upvotes: 1

Views: 7788

Answers (2)

vidit
vidit

Reputation: 6451

I believe you are trying to get the output from process, and for that you should get the InputStream.

InputStream is = Runtime.getRuntime().exec("ls").getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader buff = new BufferedReader (isr);

String line;
while((line = buff.readLine()) != null)
    System.out.print(line);

You get the OutputStream when you want to write/ send some output to Process.

Upvotes: 2

Shmil The Cat
Shmil The Cat

Reputation: 4670

Convert the stream to string as discussed in Get an OutputStream into a String and simply use Sysrem.out.print()

Upvotes: 0

Related Questions