A few months ago I read a question on the ITToolbox PeopleTools-I forum asking if it was possible to read the output of a spawned process. Unfortunately this is not possible with the delivered PeopleCode Exec function. I got to thinking about this though, and I wondered if it was possible to accomplish this by using Java's Runtime object from PeopleCode. The answer is yes. To test this, create a batch file in your c:\temp
directory named sayHello.bat
and then add the following batch file commands to this file:
@echo off
echo Hello %1
echo How are you?
Next, open App Designer and create a new App Engine program. Add a new PeopleCode action to this program and insert the following PeopleCode:
Local JavaObject &runtime = GetJavaClass("java.lang.Runtime").getRuntime();
Local JavaObject &process = &runtime.exec("c:\temp\sayHello.bat """ | %OperatorId | """");
Local JavaObject &inputStreamReader = CreateJavaObject("java.io.InputStreamReader", &process.getInputStream());
Local JavaObject &bufferedReader = CreateJavaObject("java.io.BufferedReader", &inputStreamReader);
Local any &inputLine;
While True
&inputLine = &bufferedReader.readLine();
If (&inputLine <> Null) Then
MessageBox(0, "", 0, 0, &inputLine);
Else
Break;
End-If;
End-While;
Open the new App Engine's properties and disable restart. From the App Designer menu bar, choose Edit | Run Program. When the Run Request dialog appears, select the Output Log to File checkbox and then activate the Run button. When the App Engine finishes, open the log file (usually c:\temp\NAME_OF_AE.log) and review its contents. Here is the contents of my log file. Notice that I was logged in as user PS
and named my App Engine JJM_JAVAEXEC
. I expect your results to differ slightly based on your tools version, operator ID, and program name.
PeopleTools 8.49 - Application Engine
Copyright (c) 1988-2010 PeopleSoft, Inc.
All Rights Reserved
Hello "PS" (0,0)
Message Set Number: 0
Message Number: 0
Message Reason: Hello "PS" (0,0) (0,0)
How are you? (0,0)
Message Set Number: 0
Message Number: 0
Message Reason: How are you? (0,0) (0,0)
Application Engine program JJM_JAVAEXEC ended normally
The following example is similar to the previous, but writes to stdin
as well as reading from stdout
. To run this example, you need a copy of grep. I use the version that comes with UnxUtils (Note: You do not need grep or UnxUtils to write to stdin. Grep is a common command line tool, so I used it here for example purposes only). The following code writes two lines to the grep
program's standard input (stdin): EMPLID and OPRID. It then asks grep
to find the line containing OPRID
. The PeopleCode then reads the grep
program's output and writes it to the App Engine log.
Local JavaObject &runtime = GetJavaClass("java.lang.Runtime").getRuntime();
Local JavaObject &process = &runtime.exec("grep ""OPRID""");
Local JavaObject &inputStreamReader = CreateJavaObject("java.io.InputStreamReader", &process.getInputStream());
Local JavaObject &bufferedReader = CreateJavaObject("java.io.BufferedReader", &inputStreamReader);
Local JavaObject &outputStreamWriter = CreateJavaObject("java.io.OutputStreamWriter", &process.getOutputStream());
Local JavaObject &outputBuffer = CreateJavaObject("java.io.BufferedWriter", &outputStreamWriter);
Local any &inputLine;
&outputBuffer.write("EMPLID: " | %EmployeeId);
&outputBuffer.newLine();
&outputBuffer.write("OPRID: " | %OperatorId);
&outputBuffer.close();
Repeat
&inputLine = &bufferedReader.readLine();
If (All(&inputLine)) Then
MessageBox(0, "", 0, 0, &inputLine);
End-If;
Until None(&inputLine);
If you want to run this example and have a copy of grep
in your %PATH%
environment variable, then add a new step and PeopleCode action to your App Engine, deactivate the previously created step, and then run the program. Your output should look something like:
PeopleTools 8.49 - Application Engine
Copyright (c) 1988-2010 PeopleSoft, Inc.
All Rights Reserved
OPRID: PS (0,0)
Message Set Number: 0
Message Number: 0
Message Reason: OPRID: PS (0,0) (0,0)
Application Engine program JJM_JAVAEXEC ended normally