Skip to main content

How to call AS400 RPG procedure from Java

No replies
mdfirojali2007
Offline
Joined: 2013-10-18
Points: 0

Hi Guys,

I want to write a statndalone java application that call the AS400 RPG procedure and get back me the Character of length10 as Output and
my code is attached below.

package com.ingate.test.poc;

import com.ibm.as400.access.AS400;
import com.ibm.as400.access.AS400Message;
import com.ibm.as400.access.AS400Text;
import com.ibm.as400.access.CommandCall;
import com.ibm.as400.access.ServiceProgramCall;

public class MyClass {

public static void main(String[] args) {

try {
// AS400 system details
AS400 as400 = new AS400("AS400SYSTEMNAME");
as400.setUserId("USERID");
as400.setPassword("PASSWORD");
as400.setCcsid(37);

// Initialize the service program object
ServiceProgramCall sPGMCall = new ServiceProgramCall(as400);
sPGMCall.setProgram("/SYSTEM.LIB/LIBNAME.LIB/SERVICEPROGRAMENAME.SRVPGM");
sPGMCall.setProcedureName("PROCEDURENAME");
sPGMCall.setReturnValueFormat(ServiceProgramCall.RETURN_INTEGER);

// Added another library
CommandCall command = new CommandCall(as400);
if (command.run("ADDLIBLE LIB(ANOTHERLIB)") != true) {
System.out.println("Command failed during adding library!");
}

// Execute the service program
boolean isRun = sPGMCall.run();
System.out.println("Program Call : "+isRun);
if (isRun != true) {
// Get the error messages when the call fails.
AS400Message[] messageList = sPGMCall.getMessageList();
for (int i = 0; i < messageList.length; ++i) {
System.out.println(messageList[i].getID() +" : " +messageList[i].getText());
}
} else {
// Get the returned value when the call is successful.
Object obj = new AS400Text(6).toObject(sPGMCall.getReturnValue());
System.out.println("Resulted output is : " + obj.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

and my output look like below
Program Call : true
Resulted output is : CDEF
where
AS400SYSTEMNAME : AS400 system name
USERID : AS400 system's userid
PASSWORD : AS400 system's password
PROCEDURENAME : AS400 systems procedure name
ANOTHERLIB : name of the another library that i have to add

and here my already written PROCEDURENAME in AS400 under the SERVICEPROGRAMENAME is returning 6 character(e.g Hardcoded as 'ABCDEF') but if i run or execute my attached code i am getting only 4 character and two spaces onwards(like : CDEF ) and omitting AB character.

So, can anybody help me so that i am able to get the correct output of 'ABCDEF' instead of 'CDEF' as this code is very urgent requirment?

Thanks