1

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

One Two Three
  • 20,819
  • 23
  • 69
  • 107

2 Answers2

2

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.

vidit
  • 6,135
  • 3
  • 29
  • 47
0

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

Community
  • 1
  • 1
Shmil The Cat
  • 4,505
  • 2
  • 25
  • 34