Java method call from RPG (method call in own class)

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.)

IBM i environment check

It's okay if the Java version is returned by the following command.

CHGJOB CCSID(5035)
STRQSH
java -version

Folder structure

The folder structure of IFS on IBM i is as follows.

/Root
  |-JavaApps
     |-/bin
        |-/mypackage
            |-As400Funcs.class
     |-/lib
        |-jt400.jar
     |-/src
        |-/mypackage
            |-As400Funcs.java

Java program preparation

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;
	}
}

Compile and test run Java programs

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.

RPG program preparation and compilation

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) 

Run RPG program

CALL GETJOBSTSR PARM('DSP01') 

It is OK if the job status is displayed on the screen.

"Received JAVA exception when calling JAVA method" error

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

Java method call from RPG (method call in own class)
Call Java method from JavaScript executed in Java
Call Kotlin's sealed class from Java
Call the super method in Java
Implement Java Interface in JRuby class and call it from Java
GetInstance () from a @Singleton class in Groovy from Java
Call Java from JRuby
How to get Class from Element in Java
Java programming (class method)
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
Call a method with a Kotlin callback block from Java
Write a class in Kotlin and call it in Java
Call a program written in Swift from Processing (Java)
Call your own method with PreAuthorize in Spring Security
How to get the class name / method name running in Java
[Kotlin] Get Java Constructor / Method from KFunction and call it
Call Chain from Chain in Spring Integration
[Java] Object-oriented syntax --class method / argument
Automatic photo resizing method in Java
Handle your own annotations in Java
StringBuffer and StringBuilder Class in Java
Call TensorFlow Java API from Scala
[java] What I did when comparing Lists in my own class
[Java] Wrapper that executes private method of object from outside class
Study Deep Learning from scratch in Java.
[Java] Pass arguments to constructor in Mockito / Set method default call to callRealMethod
Java method
java (method)
Call Java library from C with JNI
Call GitHub API from Java Socket API part2
Understand java interface in your own way
Call the Windows Notification API in Java
Concurrency Method in Java with basic example
[Beginner] Java method / class / external library [Note 23]
Reverse Key from Value in Java Map
Using JavaScript from Java in Rhino 2021 version
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Why does Java call a file a class?
What is the main method in Java?
Java method
Class method
Call Java methods from Nim using jnim
[Java] method
Get history from Zabbix server in Java
Creating a matrix class in Java Part 1
[Java Spring MVC] I want to use DI in my own class
Call Visual Recognition in Watson Java SDK
[Java] Instance method, instance field, class method, class field, constructor summary
Element operation method in appium TIPS (Java)
When calling println etc. from an external Java class file in Processing
[Java] method
Call a method of the parent class by explicitly specifying the name in Ruby
Increment with the third argument of iterate method of Stream class added from Java9
Cause of is not visible when calling a method of another class in java
[Java] Handling of JavaBeans in the method chain
[Java] Dynamic method call by reflection of enum (enum)
I tried hitting a Java method from ABCL
Text extraction in Java from PDF with pdfbox-2.0.8
[Java SE 11 Silver] Arrays class method summary [Java beginner]
Java starting from beginner, class declaration / object generation