How do i get the output of a command?

so i want to get the output of the following command

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?

2 Likes
1 Like

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

1 Like
1 Like

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?

1 Like

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

Now you can import it like this: :innocent:
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. :cowboy_hat_face:

1 Like

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

Kf


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();
}
2 Likes

so i have the following

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) ?

1 Like

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

Kf

1 Like

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

2 Likes