You are currently on IBM Systems Media’s archival website. Click here to view our new website.


Calling Java from RPG and Vice Versa

Prior to OS/400* V5R1, the easiest way to call Java* from RPG was to use the RUNJVA command. This calls the "main" method of a Java class. For the Java class to pass back values, you can use a data queue, data area or file.

If the Java class doesn't have a main method, you have two options:

  1. If you own and can change the class, you can add a main method.
  2. Otherwise, create a new class that can be called. The new class would call the necessary Java methods in the other class.

An easy way to call from Java to RPG is to use the AS/400 Toolbox classes. This allows a Java method to call an existing RPG program, without the program having to make changes. The Java coding required is fairly straightforward with a few cumbersome spots.

The Java native interface (JNI) is another option for calling between RPG and Java. From the Java side, using the JNI is easy; the Java class just defines the native methods and tells the system where the native method is. The following class defines a native method, getName, and indicates that this native method is found in the service program, MYSRVPGM (see code samples).

The Java side is simple, but the native (C, C++, RPG or COBOL) method is not. The actual procedure must be called, Java_MYCLASS_getName. When it's called, it will be passed two extra parameters. The first is a pointer to the JNI data structure, and the second is a class instance for the method to operate on. The rest of the parameters are fairly straightforward, unless they are Java objects. If the native method wants to return a string object, it must make several JNI calls (using procedure pointers in the JNI data structure) to create the string, not to mention translating between EBSDIC and ASCII.

Whats New?
Using V5R1 RPG IV, you can call Java methods as easily as ordinary procedures, and you can write native methods in RPG as easily as ordinary subprocedures. Well, almost. There is still extra code to write if you want to deal with a Java object, like a string.

















Hans Boldt is a member of the RPG compiler development team at IBM in Toronto, Ontario, Canada. Hans can be reached at



2019 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

Converting an IBM i Spool File to a PDF

A comprehensive four-step approach

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters