To define an RPG native method, you code the prototype the same way as you would code the prototype for an ordinary Java™ method. Then, you write the RPG subprocedure normally. You must code the EXPORT keyword on the Procedure-Begin Specification for the native method.
static
{
System.loadLibrary ("MYSRVPGM");
}
This will enable Java to find your native methods. Aside from
adding *JAVA and the class to the EXTPROC keyword for the prototype
of a native method, you write your native method like any subprocedure. Figure 1 is an example of a Java class that calls a native method. class MyClass
{
static
{
System.loadLibrary ("MYSRVPGM");
}
native boolean checkCust (byte custName[]);
void anotherMethod ()
{
boolean found;
// call the native method
found = checkCust (str.getBytes());
}
}
Figure 2 is a prototype of an RPG native method.
D checkCust PR N EXTPROC(*JAVA
D : 'MyClass'
D : 'checkCust')
D custName 100A VARYING CONST
The native method itself is coded just like any subprocedure. Figure 3 is an example of a native method coded in RPG.
P checkCust B EXPORT
D checkCust PI N
D custName 100A VARYING CONST
/free chain custName rec;
return %found;
/end-free
P checkCust E
If you create any Java objects in your native methods, by default they will be destroyed by Java when the native method returns. If you want the object to be available after the native method returns (for example, if you want to use it from another native method later), then you must tell Java that you want to make a global reference, by calling the JNI wrapper procedure getNewGlobalRef . When you are finished with the global reference, you will call JNI wrapper procedure freeGlobalRef, so Java can reclaim the object. See Telling Java you want an object to be permanent and Telling Java you are finished with a permanent object for more information about these wrapper procedures.
try
{
nativeMethod ();
}
catch (Exception exc)
{
…
}