A memorandum of calling Java methods from ILE RPG. It is also mentioned in the "IBM Rational Development Studio for i ILE RPG Programmer's Guide" (IBM i version 7.2), but it wasn't written in great detail, so I actually tried it.
The environment of IBM i that was executed is version: V5R4, Java: 1.4.2. (It also worked on V7R2 and Java 1.8.0.)
To make it a little more practical, I created a program that returns the status of running jobs. Pass a string (job name) as an argument and return the string (status). (If the same job name exists more than once, the status of the first acquired job is returned.)
It's okay if the Java version is returned by the following command.
CHGJOB CCSID(5035)
STRQSH
java -version
The folder structure of IFS on IBM i is as follows.
/Root
|-JavaApps
|-/bin
|-/mypackage
|-As400Funcs.class
|-/lib
|-jt400.jar
|-/src
|-/mypackage
|-As400Funcs.java
As400Funcs.java
package mypackage;
import com.ibm.as400.access.AS400;
import com.ibm.as400.resource.RJob;
import com.ibm.as400.resource.RJobList;
import com.ibm.as400.resource.ResourceException;
public class As400Funcs {
public static void main(String[] args) {
System.out.println(GetJobSts("DSP01"));
}
public static String GetJobSts(String searchJob) {
AS400 as400 = new AS400("192.168.XXX.XXX" ,"MYUSER" ,"MYPASS");
//init
String resultStatus = "NONE";
//get JobList
RJobList jobList = new RJobList(as400);
try {
jobList.setSelectionValue(RJobList.PRIMARY_JOB_STATUSES, new String[] { RJob.JOB_STATUS_ACTIVE });
jobList.setSelectionValue(RJobList.JOB_NAME,searchJob);
jobList.open();
jobList.waitForComplete();
//Get First Job
long numberOfJobs = jobList.getListLength();
for (long i = 0; i<numberOfJobs; ++i){
RJob ajob = (RJob)jobList.resourceAt(i);
RJob rjob = new RJob(as400,searchJob,(String)ajob.getAttributeValue(RJob.USER_NAME),(String)ajob.getAttributeValue(RJob.JOB_NUMBER));
resultStatus = (String)rjob.getAttributeValue(RJob.ACTIVE_JOB_STATUS);
System.out.println("JOB Status:"+resultStatus);
System.out.println("JOB Name:"+searchJob);
break;
}
} catch (ResourceException e) {
e.printStackTrace();
}
return resultStatus;
}
}
Set environment variables. (Please sign off once to make sure.)
ADDENVVAR ENVVAR(CLASSPATH) VALUE('/JavaApps/bin:/JavaApps/lib/jt400.jar')
Compile and test run (QSHELL)
javac -d /JavaApps/bin /JavaApps/src/mypackage/As400Funcs.java
java mypackage/As400Funcs
In the sample, the argument job name is "DSP01", Please specify the job that is actually running. It's okay if you can get the status correctly.
GETJOBSTSR.rpgle
*
*Sample program that calls a JAVA method
*
H DFTACTGRP(*NO)
D MAIN PR EXTPGM('GETJOBSTSR')
D 10A
D MAIN PI
D searchJob 10A
D newString PR O ExtProc(*JAVA:
D 'java.lang.String':
D *CONSTRUCTOR)
D Class(*JAVA:'java.lang.String')
D bytes 10A Const Varying
D GetJobSts PR O ExtProc(*JAVA:
D 'mypackage.As400Funcs'
D :'GetJobSts')
D STATIC
D CLASS(*JAVA:'java.lang.String') //Return type
D string O CLASS(*JAVA:'java.lang.String') Const//Parameters
D getBytes PR 10A ExtProc(*JAVA:
D 'java.lang.String':
D 'getBytes')
D Varying
D string1 S O Class(*JAVA:'java.lang.String')
D StringResult S O Class(*JAVA:'java.lang.String')
D StringDisply S 10A Varying
/Free
String1 = newString(searchJob) ;
StringResult = GetJobSts(String1) ;
StringDisply = getBytes(StringResult) ;
dsply StringDisply;
*inLR = *on ;
return ;
/End-Free
CRTBNDRPG PGM(MYLIB/GETJOBSTSR) SRCFILE(MYLIB/MYSRC) DBGVIEW(*SOURCE)
CALL GETJOBSTSR PARM('DSP01')
It is OK if the job status is displayed on the screen.
java.lang.NoClassDefFoundError
In the case of an error, the environment variable "CLASSPATH" is often incorrect. Especially once the RPG is executed, the JVM starts with that job, and then
ADDENVVAR ENVVAR(CLASSPATH) VALUE('/JavaApps/bin:/JavaApps/lib/jt400.jar')
** Is not reflected ** even if is executed. (It seems that you cannot restart the JVM, and you have to sign off once.)
Recommended Posts