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:
- If you own and can change the class, you can add a main method.
- 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.
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.
comments powered by