How to Call AS/400 RPG Program from Java


IBM has provided library for communicating with the AS/400 server from Java. The IBM Toolbox for Java is a library of Java classes that give Java programs easy access to IBM iSeries data and resources. JT Open is the open source version of Toolbox for Java. You can go to JT Open link to download the full set of java libraries and some more details of how that can be used to easily communicate with the AS/400 server.

In this post, I am going to demonstrate how to execute the RPG programs from Java. First thing we need are the details of an RPG program that we will be calling from our Java client. For the purpose of this exercise we created a test RPG(ANITEST) program in our AS/400 system that takes two parameters, one for input and one for output. The program takes the input parameter as the USERID returns the Name of the USERID if it exists and ERROR if it fails to find the user. Here are the details of the program:

Program Name: ANITEST.PGM
Library Name: ANITEST.LIB
PGM Location: QSYS.LIB

We have two options to call the AS400 program from our Java application. One approach is to call this program directly using the ProgramCall class and the other option is to call the program using PCML document.

We have two options to call the AS400 program from our Java application. One approach is to call this program directly using the ProgramCall class and the other option is to call the program using PCML document.

Using ProgramCall Class

One way of calling a RPG program from Java is to use the ProgramCall class in JT Open. First create an AS/400 instance and pass it to the ProgramCall constructer and then call the program with list of parameters. Let's see how the ProgramCall class is used to call this RPG program.
Let's see how the ProgramCall class is used to call this RPG program.

import com.ibm.as400.access.AS400;
import com.ibm.as400.access.AS400Message;
import com.ibm.as400.access.AS400SecurityException;
import com.ibm.as400.access.ErrorCompletingRequestException;
import com.ibm.as400.access.ObjectDoesNotExistException;
import com.ibm.as400.access.ProgramCall;
import com.ibm.as400.access.ProgramParameter;
import java.beans.PropertyVetoException;
import java.io.IOException;

/**
 * Test program to test the RPG call from Java.
 */
public class CallingAS400PGM {

    private static final String HOST = "AS400 SERVER IP";
    private static final String UID = "A S400USERNAME";
    private static final String PWD = "AS400PASSWORD";

    public static void main(String[] args) {

        String input = "ANIL";
        String fullProgramName = "/QSYS.LIB/ANITEST.LIB/ANITEST.PGM";

        AS400 as400 = null;
        byte[] inputData;
        byte[] outputData;
        ProgramParameter[] parmList;
        ProgramCall programCall;
        try {
            // Create an AS400 object
            as400 = new AS400(HOST, UID, PWD);

            // Create a parameter list
            // The list must have both input and output parameters
            parmList = new ProgramParameter[2];

            // Convert the Strings to IBM format
            inputData = input.getBytes("IBM285");

            // Create the input parameter  
            parmList[0] = new ProgramParameter(inputData);

            // Create the output parameter
            //Prarameterised Constructor is for the OUTPUT LENGTH. here it is 10
            parmList[1] = new ProgramParameter(10);

            /**
             * Create a program object specifying the name of the program and
             * the parameter list.
             */
            programCall = new ProgramCall(as400);
            programCall.setProgram(fullProgramName, parmList);

            // Run the program.  
            if (!programCall.run()) {
                /**
                 * If the AS/400 is not run then look at the message list to
                 * find out why it didn't run.
                 */
                AS400Message[] messageList = programCall.getMessageList();
                for (AS400Message message : messageList) {
                    System.out.println(message.getID() + " - " + message.getText());
                }
            } else {
                /**
                 * Else the program is successfull. Process the output, which
                 * contains the returned data.
                 */
                outputData = parmList[1].getOutputData();
                String output = new String(outputData, "IBM285").trim();

                System.out.println("Output is " + output);
            }

        } catch (PropertyVetoException | AS400SecurityException | ErrorCompletingRequestException | IOException | InterruptedException | ObjectDoesNotExistException e) {
            System.err.println(":: Exception ::" + e.toString());
        } finally {
            try {
                // Make sure to disconnect 
                if (as400 != null) {
                    as400.disconnectAllServices();
                }
            } catch (Exception e) {
                System.err.println(":: Exception ::" + e.toString());
            }
        }
    }
}

No comments

Powered by Blogger.