IBM i Global

IBM i Global

Connect, learn, share, and engage with IBM Power.

 View Only

Getting Started with JT400: Automating IBM i with Java

By Gaurav Khanna posted Mon April 28, 2025 12:51 AM

  

A beginner's guide to using JT400 for basic IBM i automation

Introduction

IBM i has long been a powerful platform for business-critical applications, and with JT400, a Java-based open-source library, you can extend that power by automating common tasks from any Java-enabled environment. Whether you're managing spool files, executing CL commands, or transferring files, JT400 gives you the APIs to make it happen.

What is JT400?

JT400 is a Java toolkit developed by IBM and maintained by the open-source community. It allows Java applications to communicate with IBM i systems using TCP/IP. It’s part of the IBM Toolbox for Java, and supports a wide range of features:

Key Benefits:

  • Platform-independent (works on any system with Java)
  • No need for IBM i client installation
  • Supports database access, file transfer, program calls, command execution, and more
  • Easy integration with Jenkins or other Java-based CI/CD tools

Also check out: The Jenkins plugin IBM i Steps — it’s built on JT400 and simplifies automation for Jenkins pipelines.

Common Use Cases for JT400

  • Calling IBM i Programs (CL, RPG)
  • Running CL Commands (e.g., CRTSAVF, RSTOBJ)
  • File Transfer to Libraries (upload .SAVF, .CLP, .TXT)
  • Reading Spool Files
  • Submitting Batch Jobs

Quick Start Tip: Save any of the below examples as MyIBMi.java in a folder along with jt400.jar, update "myibmi.company.com", "MYUSER", "MYPASSWORD", and run it using: java -cp jt400.jar;. MyIBMi

Sample 1: Connect and Run a CL Command

import com.ibm.as400.access.*;

public class MyIBMi {

    public static void main(String[] args) throws Exception {

        AS400 system = new AS400("myibmi.company.com", "MYUSER", "MYPASSWORD");

        CommandCall cmd = new CommandCall(system);

        if (cmd.run("CRTSAVF FILE(QGPL/MYLIBSAVF)")) {

            System.out.println("Save file created successfully.");

        } else {

            AS400Message[] messageList = cmd.getMessageList();

            for (AS400Message msg : messageList) {

                System.out.println("Error: " + msg.getText());

            }

        }

        system.disconnectAllServices();

    }

}

Sample 2: Upload a File to the IBM i IFS

import java.io.*;

import com.ibm.as400.access.*;

public class MyIBMi {

    public static void main(String[] args) throws Exception {

        AS400 system = new AS400("myibmi.company.com", "MYUSER", "MYPASSWORD");

        IFSFile remoteFile = new IFSFile(system, "/QSYS.LIB/QGPL.LIB/CSBASE.FILE");

        IFSFileOutputStream outStream = new IFSFileOutputStream(remoteFile);

        File localFile = new File("C:/local/CSBASE.FILE");

        FileInputStream inStream = new FileInputStream(localFile);

        byte[] buffer = new byte[1024];

        int bytesRead;

        while ((bytesRead = inStream.read(buffer)) != -1) {

            outStream.write(buffer, 0, bytesRead);

        }

        inStream.close();

        outStream.close();

        system.disconnectAllServices();

        System.out.println("File uploaded successfully.");

    }

}

Sample 3: Read a Spool File

import com.ibm.as400.access.*;

public class MyIBMi {

    public static void main(String[] args) throws Exception {

        AS400 system = new AS400("myibmi.company.com", "MYUSER", "MYPASSWORD");

        SpooledFileList splfList = new SpooledFileList(system);

        splfList.openSynchronously();

        for (int i = 0; i < splfList.size(); i++) {

            SpooledFile sf = (SpooledFile) splfList.getObject(i);

            System.out.println("Spool File: " + sf.getName() + ", Job: " + sf.getJobName());

        }

        splfList.close();

        system.disconnectAllServices();

    }

}

Sample 4: Call an IBM i Program

import com.ibm.as400.access.*;

public class MyIBMi {

    public static void main(String[] args) throws Exception {

        AS400 system = new AS400("myibmi.company.com", "MYUSER", "MYPASSWORD");

        ProgramCall pgm = new ProgramCall(system);

        // Call a program with no parameters

        pgm.setProgram("/QSYS.LIB/QGPL.LIB/MYPGM.PGM");

        if (pgm.run()) {

            System.out.println("Program executed successfully.");

        } else {

            AS400Message[] messageList = pgm.getMessageList();

            for (AS400Message msg : messageList) {

                System.out.println("Error: " + msg.getText());

            }

        }

        system.disconnectAllServices();

    }

}

Tips for Getting Started

  • Download the latest jt400.jar from jt400.sourceforge.net
  • Use Java 8 or higher
  • Include JT400 in your classpath when compiling and running Java code
  • Start with simple command calls, then move on to program calls and file operations

JT400 in CI/CD

JT400 is ideal for build automation:

  • Use in Jenkins pipelines to upload/install IBM i objects
  • Automate deployment of .SAVF, .PGM, .CLLE files
  • Trigger CL scripts before/after build steps

Try the IBM i Steps Jenkins Plugin to simplify automation -- it wraps JT400 logic into easy-to-use Jenkins steps.

Conclusion

JT400 is a powerful bridge between modern Java tools and the IBM i world. With a few lines of code, you can automate common system operations, streamline testing, and integrate IBM i into your existing automation workflows.

0 comments
11 views

Permalink