Calling Java™ Methods Using the JNI Rather than RPG *JAVA Prototypes
The first three parameters are always the same:
- the JNI environment pointer
- the object (for instance methods) or the class (for static methods)
- the method
The method-specific parameters are coded after these three parameters,
in one of three different ways. For example, if the method does not
return a value (the return type is "void"),
- CallVoidMethod:
- Choose this way if you are going to call the same method many
times, since it makes the method very easy to call. This expects the
parameters to be passed normally. To call this JNI function, an RPG programmer would copy
the CallVoidMethod prototype from the JNI /COPY file, and code additional
parameters. These functions require at least one parameter
to be coded with OPTIONS(*NOPASS). If you don't want to make the method
parameters optional, add an extra “dummy” parameter with OPTIONS(*NOPASS). For
example, for the method
you could code the following prototype for CallVoidMethod:void mymethod (int len, String str);Figure 1. Sample RPG Code for Calling CallVoidMethodD CallMyMethod PR EXTPROC(*CWIDEN D : JNINativeInterface. D CallVoidMethod_P) D env LIKE(JNIEnv_P) VALUE D obj LIKE(jobject) VALUE D methodID LIKE(jmethodID) VALUE D len LIKE(jint) VALUE D str LIKE(jstring) CONST D dummy 1a OPTIONS (*NOPASS) … CallMyMethod (JNIEnv_P : objectId : methodId : 10 : string); - CallVoidMethodA:
- Choose this way if you do not want to create a separate prototype for calling a method. This expects an array of jvalue structures, with each element of the array holding one parameter. Figure 1 above is an example of this.
- CallVoidMethodV:
- Do not use this in RPG code. It expects a C construct that is extremely awkward to code in RPG.
The actual function to call depends on the type of the return value. For example, if the method returns an integer, you would use CallIntMethodA. To get the class and methodID parameters for these functions, use the FindClass and GetMethodID or GetStaticMethodID.
Note: When calling the JNI directly, the class names must be specified
with a slash (/) rather than a period (.) as the separator. For example,
use 'java/lang/String' rather than 'java.lang.String'.