Executing System Commands from PeopleCode and Getting STDOUT

By Chris Malek | Sun, May 15, 2016

I recently had a need to execute some OS system level calls in PeopleCode but I needed to get the output of the command call. I did some googling and found that some other bloggers had already done this. I am documenting this on my own blog as I can think of many future uses of this for some code I plan on releasing this year.

I actually needed to get the OS version for Oracle support. However, I did not want to wait for a response from the system admins. So I wrote a quick function in my Unit Test Page to return the OS version.

Here is the PeopleCode.

Function getOSVersion() returns string
   &commandPath = "cat /etc/redhat-release";

   Local JavaObject &runtime = GetJavaClass("java.lang.Runtime").getRuntime();

   Local JavaObject &process = &runtime.exec(&commandPath);

   Local JavaObject &inputStreamReader = CreateJavaObject("java.io.InputStreamReader", &process.getInputStream());
   Local JavaObject &bufferedReader = CreateJavaObject("java.io.BufferedReader", &inputStreamReader);
   Local integer &returnCode = &process.waitFor();

   Local any &inputLine;
   Local any &fullOutput;

   While True
      &inputLine = &bufferedReader.readLine();
      If (&inputLine <> Null) Then
         &fullOutput = &fullOutput | &inputLine;

    return &fullOutput ;


This function gets the Linux OS version of a RedHat server by calling the operating system CAT command with some parameters.

The full command is: cat /etc/redhat-release

You could substitute any executable in that process and get the results.

If you need to look for the return code you can see this line from above.

Local integer &returnCode = &process.waitFor();

My example code does not do anything with the &returnCode but if the executable you are calling relies on return codes you can use that integer to respond to the return code.

Additional Reading

I have to give full credit to Jim Marion as I did not know this was possible until a google search found that he had written a great article which this code was adopted from called AppEngine Output Tricks, Reporting, Logging, Etc.

Also @vindac wrote a short post Execute shell commands using Java Runtime in PeopleCode that has a slight variation that was also helpful.

Article Categories
Integration Broker - The Missing Manual

I am in the process of writing a book called "Integration Broker - The Missing Manual" for which you can read online for free.

Read Online