Program call

The ProgramCall class allows the Java program to call an AS/400 program. You can use the ProgramParameter class to specify input, output, and input/output parameters. If the program runs, the output and input/output parameters contain the data that is returned by the AS/400 program. If the AS/400 program fails to run successfully, the Java program can retrieve any resulting AS/400 messages as a list of AS400Message objects.

Required parameters are as follows:

The program name and parameter list can be set on the constructor, through the setProgram() method, or on the run() method The run() method calls the program.

The ProgramCall object class causes the AS400 object to connect to the AS/400.

The following example shows how to use the ProgramCall class:

                       // Create an AS400 object.
     AS400 sys = new AS400("mySystem.myCompany.com");

                       // Create a program object. I choose
                       // to set the program to run later.
     ProgramCall pgm = new ProgramCall(sys);

                       // Set the name of the program.
                       // Because the program does not take
                       // any parameters, pass null for the
                       // ProgramParameter[] argument.
     pgm.setProgram(QSYSObjectPathName.toPath("MYLIB",
                                              "MYPROG",
                                              "PGM"),null;

                       // Run the program. My program has
                       // no parms. If it fails, the failure
                       // is returned as a set of messages
                       // in the message list.
     if (pgm.run() != true)
     {
                       // If you get here, the program
                       // failed to run. Get the list of
                       // messages to determine why the
                       // program didn't run.
        AS400Message[] messageList = pgm.getMessageList();

                       // ... Process the message list.
     }

                       // Disconnect since I am done
                       // running programs
     sys.disconnectService(AS400.COMMAND);

The ProgramCall object requires the integrated file system path name of the program.

Using the ProgramCall class causes the AS400 object to connect to the AS/400. See managing connections for information about managing connections.

Using ProgramParameter objects

You can use the ProgramParameter objects to pass parameter data between the Java program and the AS/400 program. Set the input data with the setInputData() method. After the program is run, retrieve the output data with the getOutputData() method. Each parameter is a byte array. The Java program must convert the byte array between Java and AS/400 formats. The data conversion classes provide methods for converting data. Parameters are added to the ProgramCall object as a list.

The following example shows how to use the ProgramParameter object to pass parameter data.

                       // Create an AS400 object
     AS400 sys = new AS400("mySystem.myCompany.com");

                       // My program has two parameters.
                       // Create a list to hold these
                       // parameters.
     ProgramParameter[] parmList = new ProgramParameter[2];

                       // First parameter is an input
                       // parameter
     byte[] name = {1, 2, 3};
     parmList[0] = new ProgramParameter(key);

                       // Second parameter is an output
                       // parameter. A four-byte number
                       // is returned.
     parmList[1] = new ProgramParameter(4);

                       // Create a program object
                       // specifying the name of the
                       // program and the parameter list.
     ProgramCall pgm = new ProgramCall(sys,
                                       "/QSYS.LIB/MYLIB.LIB/MYPROG.PGM",
                                       parmList);

                       // Run the program.
     if (pgm.run() != true)
     {

                       // If the AS/400 cannot run the
                       // program, look at the message list
                       // to find out why it didn't run.
        AS400Message[] messageList = pgm.getMessageList();

     }
     else
     {
                       // Else the program ran. Process the
                       // second parameter, which contains
                       // the returned data.

                       // Create a converter for this
                       // AS/400 data type
        AS400Bin4 bin4Converter = new AS400Bin4();

                       // Convert from AS/400 type to Java
                       // object. The number starts at the
                       // beginning of the buffer.
        byte[] data = parmList[1].getOutputData();
        int i = bin4Converter.toInt(data);
     }

                       // Disconnect since I am done
                       // running programs
     sys.disconnectService(AS400.COMMAND);

Example

Call the AS/400 program that gets the status of the system.


[ Legal | AS/400 Glossary ]