# How do i get the output of a command?

**URL:** https://discourse.processing.org/t/how-do-i-get-the-output-of-a-command/769
**Category:** Coding Questions
**Created:** [June 7, 2018, 2:45pm UTC](https://discourse.processing.org/t/how-do-i-get-the-output-of-a-command/769 "2018-06-07T14:45:24Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![crazylogic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/crazylogic/32/508_2.png) [@crazylogic](https://discourse.processing.org/u/crazylogic)
#### Post date: [June 7, 2018, 2:45pm UTC](https://discourse.processing.org/t/how-do-i-get-the-output-of-a-command/769/1 "2018-06-07T14:45:24Z")

</div>

so i want to get the output of the following command

```auto
Process p = exec("/usr/sbin/system_profiler", "SPHardwareDataType | grep Serial");
       try {
         int result = p.waitFor();
         println("the process returned " + result);
        } catch (InterruptedException e) { }

```

the command runs and i get a result of 0 - but i want the text as if i were in a terminal - can anyone help or point me to a working example?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [June 7, 2018, 2:56pm UTC](https://discourse.processing.org/t/how-do-i-get-the-output-of-a-command/769/2 "2018-06-07T14:56:24Z")

</div>

> <https://github.com/GoToLoop/command/blob/patch-1/src/deadpixel/command/Command.java>

---

<div class="post-metadata">

### Author: ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)
#### Post date: [June 7, 2018, 8:15pm UTC](https://discourse.processing.org/t/how-do-i-get-the-output-of-a-command/769/3 "2018-06-07T20:15:41Z")

</div>

@GoToLoop Can you please provide a description of the code you’re posting?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [June 7, 2018, 8:34pm UTC](https://discourse.processing.org/t/how-do-i-get-the-output-of-a-command/769/4 "2018-06-07T20:34:07Z")

</div>

- [https://Forum.Processing.org/two/discussion/23471/combining-two-pieces-of-code/p2#Item\_51](https://Forum.Processing.org/two/discussion/23471/combining-two-pieces-of-code/p2#Item_51)

---

<div class="post-metadata">

### Author: ![crazylogic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/crazylogic/32/508_2.png) [@crazylogic](https://discourse.processing.org/u/crazylogic)
#### Post date: [June 8, 2018, 2:00pm UTC](https://discourse.processing.org/t/how-do-i-get-the-output-of-a-command/769/5 "2018-06-08T14:00:21Z")

</div>

This thread doesn’t appear to answer my question - what am i missing here?

The link to github looks like a file to extend the exec function - but how do i use that within a processing sketch?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [June 8, 2018, 5:33pm UTC](https://discourse.processing.org/t/how-do-i-get-the-output-of-a-command/769/6 "2018-06-08T17:33:59Z")

</div>

> [@crazylogic](#):
>
> but how do i use that within a processing sketch?

It’s a “.java” file. You can download it and then drag & drop into your sketch: 🤑  
[https://Raw.GitHubUserContent.com/GoToLoop/command/patch-1/src/deadpixel/command/Command.java](https://Raw.GitHubUserContent.com/GoToLoop/command/patch-1/src/deadpixel/command/Command.java)

Now you can `import` it like this: 😇  
`import deadpixel.command.Command;`

You can check out the link I’ve posted from my previous reply in order to figure out how to use it. 🤠

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [June 9, 2018, 8:22am UTC](https://discourse.processing.org/t/how-do-i-get-the-output-of-a-command/769/7 "2018-06-09T08:22:42Z")

</div>

Tested this code below in Win10. Command is executed and the output of this command is piped to a text file.

Kf

```auto

import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

import java.util.List;
import java.util.ArrayList;

//REFERENCE: https://www.tutorialspoint.com/java/lang/process_geterrorstream.htm
//REFERENCE: https://discourse.processing.org/t/how-do-i-get-the-output-of-a-command/769
//REFERENCE: http://www.codecodex.com/wiki/Execute_an_external_program_and_capture_the_output

ArrayList<String> outputBuffer = new ArrayList<String>();
Process p=null;
InputStream error=null;

try {

  String[] pars ={"cmd", "/c", "dir", "/D", ">>C:\\mySandBox\\procdemo.txt"};

  // create a new process
  System.out.println("Creating Process... [Executing 1 of 4 possible cases]");
  //p = Runtime.getRuntime().exec("notepad.exe"); // An example of good case GOOD 
  //p = Runtime.getRuntime().exec("nnotepad.exe"); // Example on ERROR: Typo in name
  //p = Runtime.getRuntime().exec("cmd /c dir /D >> C:\\mySandBox\\procdemo.txt"); //Custom cmd
  p = Runtime.getRuntime().exec(pars); //Custom cmd //Same as before

  final BufferedReader
    out = new BufferedReader(new InputStreamReader(p.getInputStream())), 
    err = new BufferedReader(new InputStreamReader(p.getErrorStream()));

  outputBuffer.clear();
  String read;
  while ((read = out.readLine()) != null) outputBuffer.add(read);

  System.out.println("Size out buffer: " + outputBuffer.size());
  for (String ss : outputBuffer) {
    System.out.println("WATCH2 : " + ss);
  }

  // get the error stream of the process and print it
  error = p.getErrorStream();

  if (error.available() > 0) {
    System.out.println("ERROR DETECTED: ");
    for (int i = 0; i < error.available(); i++) {
      System.out.print( ((char) error.read()));
    }
    System.out.println();
  }

  // wait for 10 seconds and then destroy the process
  Thread.sleep(4000);
  p.destroy();
} 
catch (Exception ex) {
  ex.printStackTrace();
}

```

---

<div class="post-metadata">

### Author: ![crazylogic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/crazylogic/32/508_2.png) [@crazylogic](https://discourse.processing.org/u/crazylogic)
#### Post date: [June 19, 2018, 7:48pm UTC](https://discourse.processing.org/t/how-do-i-get-the-output-of-a-command/769/8 "2018-06-19T19:48:26Z")

</div>

so i have the following

```auto
ArrayList<String> outputBuffer = new ArrayList<String>();
Process p=null;
InputStream error=null;

try {

  // create a new process
  System.out.println("Creating Process... [Executing 1 of 4 possible cases]");
  //p = Runtime.getRuntime().exec("notepad.exe"); // An example of good case GOOD 
  //p = Runtime.getRuntime().exec("nnotepad.exe"); // Example on ERROR: Typo in name
  //p = Runtime.getRuntime().exec("cmd /c dir /D >> C:\\mySandBox\\procdemo.txt"); //Custom cmd
  p = exec("ping", "-c 4 1.1.1.1");

  final BufferedReader
    out = new BufferedReader(new InputStreamReader(p.getInputStream())), 
    err = new BufferedReader(new InputStreamReader(p.getErrorStream()));

  outputBuffer.clear();
  String read;
  while ((read = out.readLine()) != null) outputBuffer.add(read);

  System.out.println("Size out buffer: " + outputBuffer.size());
  for (String ss : outputBuffer) {
    System.out.println("WATCH2 : " + ss);
  }

  // get the error stream of the process and print it
  error = p.getErrorStream();

  if (error.available() > 0) {
    System.out.println("ERROR DETECTED: ");
    for (int i = 0; i < error.available(); i++) {
      System.out.print( ((char) error.read()));
    }
    System.out.println();
  }

  // wait for 10 seconds and then destroy the process
  Thread.sleep(4000);
  p.destroy();
} 
catch (Exception ex) {
  ex.printStackTrace();
}

```

but it gives me the following error.

> Creating Process… [Executing 1 of 4 possible cases]  
> Size out buffer: 0  
> ERROR DETECTED:  
> ping: invalid count of packet

Any ideas why (on a mac platform) ?

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [June 19, 2018, 10:43pm UTC](https://discourse.processing.org/t/how-do-i-get-the-output-of-a-command/769/9 "2018-06-19T22:43:41Z")

</div>

Sorry. I do not own a mac. I won’t be able to help any further.

Kf

---

<div class="post-metadata">

### Author: ![crazylogic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/crazylogic/32/508_2.png) [@crazylogic](https://discourse.processing.org/u/crazylogic)
#### Post date: [June 19, 2018, 11:45pm UTC](https://discourse.processing.org/t/how-do-i-get-the-output-of-a-command/769/10 "2018-06-19T23:45:49Z")

</div>

just found a bit of code elsewhere  
replacing

> outputBuffer.clear();  
> String read;  
> while ((read = out.readLine()) != null) outputBuffer.add(read);

with

> String read;  
> while ((read = out.readLine()) != null)  
> {  
> System.out.println(read);  
> }

gave me something i can now work with
