2

I am trying to call an RPG Logon program on an AS400 system from JAVA. The issue is whenever I give incorrect parameters, I get a response, such as user ID is incorrect, password is incorrect. When I give an incorrect path to the program, I do get the response saying "Object does not exist." However, when all the parameters are right, I don't get any response and the java program keep running without ending. What could be wrong? Code below:

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 = "xxxxxx";
    private static final String UID = "yyyyyyy";
    private static final String PWD = "zzzzzzz";

    public static void main(String[] args) {

        String input = "testuser";
        String fullProgramName = "/QSYS.lib/SEOBJP10.lib/LOGON.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.
                 */
                System.out.println("CONNECTION IS SUCCESSFUL");
                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());
            }
        }
    }
}
Harald K
  • 26,314
  • 7
  • 65
  • 111
Bazooka
  • 29
  • 1
  • 4
  • 1
    You don't give us any information on `LOGON.PGM`. Are you sure it is the Java that is running without ending, or is it `LOGON` that is running and waiting for something, maybe user input. I don't see any loops in the Java – jmarkmurphy Mar 30 '18 at 13:28
  • 1
    Hey guys, it indeed was due to the LOGON.pgm that was waiting for some input. I used the PCML method of passing the inputs and was able to successfully call the program. Thanks @jmarkmurphy – Bazooka Apr 02 '18 at 10:29
  • which jar is used for this @Bazooka,can you please the list of jars used to achieve this , we need to call RPG program from Java – rinilnath Jun 03 '20 at 12:25

1 Answers1

0

Carefully think about what parameter type,number of parameters are accepting from AS400 RPG program, and use communication only USERID.

import com.ibm.as400.access.AS400;
import com.ibm.as400.access.AS400Message;
import com.ibm.as400.access.AS400Text;
import com.ibm.as400.access.ProgramCall;
import com.ibm.as400.access.ProgramParameter;

public class CallingAS400PGM {

private static final String HOST = "192.168.1.1";//AS400 IP
private static final String UID = "UNAME"; //userid
private static final String PWD = "PWORD"; //password

public static void main(String[] args) {
//AS400 RPG progam path
String fullProgramName = "/QSYS.LIB/PBFORM12.LIB/PBFORM12CL.PGM";

AS400 as400 = null;
ProgramParameter[] parmList;//parameter list witch is accepting AS400 RPG program
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
    AS400Text nametext1 = new AS400Text(2);
    AS400Text nametext2 = new AS400Text(200);

    // Create the input parameter // get the exact patameter type and 
    length, if not this not be working  
    parmList[0] = new ProgramParameter(nametext1.toBytes("1"),2);
    parmList[1] = new ProgramParameter(nametext2.toBytes("Ravinath 
    Fernando"),200);
    // Create the output parameter

    programCall = new ProgramCall(as400);
    programCall.setProgram(fullProgramName, parmList);

    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 {
        System.out.println("success");
        /**
         * Else the program is successfull. Process the output, which
         * contains the returned data.
         */
        //use same parameter type which will be return from AS400 program
        AS400Text text1 = new AS400Text(2);
        System.out.println(text1.toObject(parmList[0].getOutputData()));
        AS400Text text2 = new AS400Text(200);
        System.out.println(text2.toObject(parmList[1].getOutputData()));
    }
    as400.disconnectService(AS400.COMMAND);
    //-----------------------
} catch (Exception e) {
    e.printStackTrace();
    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());
    }
}
}
}
Ravinath
  • 1,620
  • 1
  • 14
  • 8