RPG Native Methods
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.Figure 2 is a prototype of an RPG native method.
The native method itself is coded just like any subprocedure. Figure 3 is an example of a native method coded in RPG.
- If you are debugging your native methods, and you want to make a change to the code, you will have to sign off and sign back on again before Java will see the new version.
- If you are calling other procedures in the service program from other RPG code that is not running in the default activation group, then you will not be able to share any global variables between the "ordinary procedures" and the native methods. This scenario can arise if a procedure in your RPG service program sets up some global variables, and then calls a Java class which then calls a native method in that service program. Those native methods will not see the same data that the first procedure set up.
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.
RPG nnnnn
, where nnnnn
is
the RPG status code. try
{
nativeMethod ();
}
catch (Exception exc)
{
…
}