Blog Archive

Call RPGLE from Java using Package com.ibm.as400.access

The "Toolbox" JDBC driver jt400.jar is shipped as part of the IBM Toolbox for Java (57xxJC1). It is implemented by making direct socket connections to the database host server. This happens to be the same route that the IBM System i™ Access for Windows ODBC driver takes. However, IBM System i™ Access for Windows is NOT required. The Toolbox runs on any JVM. The URL subprotocol is as400. The class name to register is com.ibm.as400.access.AS400JDBCDriver.

  • You need the jt400.jar in your classpath for the project. You can extract a copy from the iSeries IFS directory.
  • /QIBM/ProdData/HTTP/Public/jt400/lib/jt400.jar

Here is a sample code to call a RPGLE program on the iSeries(AS400) or also know as IBM System i™. You can use the same logic for a CL program.

RPGLE sample program to be called by Java

The program takes 4 parameters:
  • Input First Name 20 char
  • Input Last Name 20 char
  • Input Age 3,0 Zoned Numeric
  • Output is a Message putting together all the input values 100 char
d $first_name     s             20                                           
d $last_name      s             20                                           
d $my_age         s              3s 0                                        
d $message        s            100                                           
                                                                             
c     *entry        plist                                                    
c                   parm                    $first_name                      
c                   parm                    $last_name                       
c                   parm                    $my_age                          
c                   parm                    $message                         
                                                                             
 /free                                                                       
                                                                             
    $message = 'My name is ' + %trim($first_name) + ' ' +                    
                               %trim($last_name) + ' age ' +                 
                               %trim(%editc($my_age:'Z')) + ' years !';      
                                                                             
    *inlr = *on;                                                             
                                                                             
 /end-free   

Java sample program calling the RPGLE


The java main () takes 3 arguments:
  • First Name (String)
  • Last Name (String)
  • Age (String)
package com.as400samplecode;

import com.ibm.as400.access.*;

public class CallRPGProgram {
 
 public static void main( String[] args ){
  String firstName = args[0].trim();
  String lastName = args[1].trim();
  String age = args[2].trim();
  String message = "";

  //Connect to the iSeries using hostname, userid and password

  AS400 as400System = new AS400({host_name or IP address},{user_id},{password});

  //The ProgramCall class allows a user to call an iSeries server program, 
  //pass parameters to it (input and output), and access data returned in the 
  //output parameters after the program runs. Use ProgramCall to call programs.
  ProgramCall program = new ProgramCall(as400System);

  try
  {
   // Initialize the name of the program to run.
   String programName = "/QSYS.LIB/{your_library}.LIB/{RPGLE or CL program name}.PGM";
   // Set up the 3 parameters.
   ProgramParameter[] parameterList = new ProgramParameter[4];

   // Parameter 1 is the First Name
   AS400Text textData = new AS400Text(20, as400System);
   parameterList[0] = new ProgramParameter(textData.toBytes(firstName));

   // Parameter 2 is the Last Name
   textData = new AS400Text(20, as400System);
   parameterList[1] = new ProgramParameter(textData.toBytes(lastName));

   // Parameter 3 is the Age
   AS400ZonedDecimal as400ZonedDecimal = new AS400ZonedDecimal(3,0) ;
   double myAge = Double.valueOf(age);
   parameterList[2] = new ProgramParameter(as400ZonedDecimal.toBytes(myAge));

   // Parameter 4 to get the answer, up to 100 bytes long.
   parameterList[3] = new ProgramParameter(100);

   // Set the program name and parameter list.
   program.setProgram(programName, parameterList);

   // Run the program.
   if (program.run() != true)
   {
    // Report failure.
    System.out.println("Program failed!");
    // Show the messages.
    AS400Message[] messageList = program.getMessageList();
    for (int i = 0; i < messageList.length; ++i)
    {
     // Show each message.
     System.out.println(messageList[i].getText());
     // Load additional message information.
     messageList[i].load();
     //Show help text.
     System.out.println(messageList[i].getHelp());
    }
   }

   // Else no error, get output data.
   else
   {
    textData = new AS400Text(100, as400System);
    message = (String) textData.toObject(parameterList[3].getOutputData());

   }
  }
  catch (Exception e)
  {
   System.out.println("Program " + program.getProgram() + " issued an exception!");
   e.printStackTrace();
  }

  // Done with the server.
  as400System.disconnectAllServices();

  //Print the output from the RPGLE called program 
  System.out.println("Message is: " + message);

 }
}

Testing Results from the above application

Java Call: java com.as400samplecode.CallRPGProgram "John" "Doe" "25"
Output: Message is: My name is John Doe age 25 years !

Reference: API Docs for Package com.ibm.as400.access