Friday, April 12, 2013

Calling AS/400 CL Commands from Java

Effective SOA solutions take advantage of all the services available across the systems, which include the legacy systems in your organization. There are several ways you can integrate the existing services running on your legacy systems. This series of posts is about using the AS/400 services directly in your Java applications. I will be covering how to call RPG programs, AS/400 Commands and AS/400 DataQueues in your Java applications.

Fortunately IBM has provided a very nice easy to use 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. There are several ways you can access the services on AS/400 server, most common are as follows.

All of these different methods of accessing the AS/400 services and have their own pros and cons. JT Open is a very powerful library and provides very easy to use APIs to communicate with the AS/400 services. These posts are about exploiting the JT Open libraries to use the AS/400 services. I am splitting these different approaches to separate posts. Click on the topic above to see the relevant post for that topic.

  • Calling AS/400 Commands.

In this article, we shell see how the CommandCall class work to execute AS/400 Commands from Java. First thing we need are the details of an AS/400 Command that we will be executing from our Java client. For the purpose of this exercise we created a test AS/400 Command that executes a batch job on the AS/400 System. Here are the details of the program:


NOTE: You can call any iSeries server CL command.

package as400;

import java.util.Date;


 * Test program to test the AS/400 Command from Java.
public class AS400CommandCallTest {

 public static void main(String[] args) {  

  String server="";
  String user = "AS400USER";
  String pass = "AS400PWRD";

  String commandStr = "MYLIB/RUNMYJOB SOME(PARAM)";

  AS400 as400 = null;
  try  {
   // Create an AS400 object  
   as400 = new AS400(server, user, pass);  
   // Create a Command object
   CommandCall command = new CommandCall(as400);

   // Run the command.
   System.out.println("Executing: " + commandStr);
   boolean success =;
   if (success) {  
    System.out.println("Command Executed Successfully.");
    System.out.println("Command Failed!");
   // Get the command results
   AS400Message[] messageList = command.getMessageList();
   for (AS400Message message : messageList){
  } catch (Exception e) {  
    // Make sure to disconnect   
   }catch(Exception e){}

And here is the out put of this program when you run it.

Command Executed Successfully.

All the command parameters are passed as a space saparated list with the command. We check the value returned by; to see if the command was successful and display the appropriate message. Calling the command.getMessageList(); will always return a list of messages if gnerated by the AS/400 Command. They can be either failure or success messages depending on if the command succeded or failed.

Note that the; will only return data if a completion message was generated by the command you are executing. Some commands do not generate a completion message if they run successfully. If you want the CommandCall to return a message regardless of its success or failure, you could do the following:

Create a CL program that will run the necessary command(s).

Include the SNDPGMMSG command as follows:
Note: The MSGTYPE parameter must be set to *COMP.

Now create a CL command that will call your CL program.

What you have done here is actually wrapped the AS/400 Command inside your custom command. The new CL command will allow you to receive a meaningful completion message in your JAVA program.

These are the very basics of how to use the IBM Toolbox for Java to communicate with programs in AS/400. For more details and advanced topics you can consult the IBM programmers guide. To view or download the PDF version of this document, select IBM® Toolbox for Java™ (about 3100 KB).

No comments:

Post a Comment