The first sample program is yet another variation of the "Hello World!" program.
The "Hello World!" program has one Java class, callingPLI.java. The native method, written in PL/I, is contained in hiFromPLI.pli. Here is a brief overview of the steps for creating this sample program:
All methods, whether Java methods or native methods, must be declared within a Java class. The only difference in the declaration of a Java method and a native method is the keyword native. The native keyword tells Java that the implementation of this method will be found in a native library that will be loaded during the execution of the program. The declaration of the native method looks like this:
public native void callToPLI();
In the above statement, the void means that there is no return value expected from this native method call. The empty parentheses in the method name callToPLI( ), means that there are no parameters being passed on the call to the native method.
A step that loads the native library must be included so the native library will be loaded at execution time. The Java statement that loads the native library looks like this:
static {
System.loadLibrary("hiFromPLI");
}
In the above statement, the Java System method System.loadLibrary(...) is called to find and load the native library. The PL/I shared library, libhiFromPLI.so, will created during the step that compiles and links the PL/I program.
The callingPLI class also includes a main method to instantiate the class and call the native method. The main method instantiates callingPLI and calls the callToPLI() native method.
The complete definition of the callingPLI class, including all the points addressed above in this section, looks like this:
public class callingPLI {
public native void callToPLI();
static {
System.loadLibrary("hiFromPLI");
}
public static void main(String[] argv) {
callingPLI callPLI = new callingPLI();
callPLI.callToPLI();
System.out.println("And Hello from Java, too!");
}
}
Use the Java compiler to compile the callingPLI class into an executable form. The command would look like this:
javac callingPLI.java
The PL/I implementation of the native method looks much like any other PL/I subroutine.
The sample program contains a series of *PROCESS statements that define the important compiler options.
*Process Limits( Extname( 100 ) ) Margins( 1, 100 ) ; *Process Display(Std) Dllinit Extrn(Short); *Process Rent Default( ASCII IEEE );
Here is a brief description of them and why they are useful:
IEEE specifies that FLOAT data is held in IEEE format - the form in which it is held by JAVA
The PL/I procedure name must conform to the Java naming convention in order to be located by the Java Class Loader at execution time. The Java naming scheme consists of three parts. The first part identifies the routine to the Java environment, the second part is the name of the Java class that defines the native method, and the third part is the name of the native method itself.
Here is a breakdown of PL/I procedure name Java_callingPLI_callToPLI in the sample program:
In addition, the following options need to be specified in the OPTIONS option on the PROCEDURE statement:
The complete procedure statement for the sample program looks like this:
Java_callingPLI_callToPLI:
Proc( JNIEnv , MyJObject )
External( "Java_callingPLI_callToPLI" )
Options( FromAlien NoDescriptor ByValue );
The two PL/I include files which contain the PL/I definitions of the Java Native interface are ibmzjni.inc which in turn includes ibmzjnim.inc. These include files are included with this statement:
%include ibmzjni;
The ibmzjni and ibmzjnim include files are provided in the PL/I SIBMZSAM data set.
For completeness, here is the entire PL/I program that defines the native method:
*Process Limits( Extname( 100 ) ) Margins( 1, 100 ) ;
*Process Display(Std) Dllinit Extrn(Short);
*Process Rent Default( ASCII IEEE );
PliJava_Demo: Package Exports(*);
Java_callingPLI_callToPLI:
Proc( JNIEnv , MyJObject )
External( "Java_callingPLI_callToPLI" )
Options( FromAlien NoDescriptor ByValue );
%include ibmzjni;
Dcl myJObject Type jObject;
Display('Hello from Enterprise PL/I!');
End;
Compile the PL/I sample program with the following command:
pli -c hiFromPLI.pli
Link the resulting PL/I object deck into a shared library with this command:
c89 -o libhiFromPLI.so hiFromPLI.o
Be sure to include the lib prefix on the name of the PL/I shared library or the Java class loader will not find it.