jPassInt クラスには、クラスのインスタンスを生成して ネイティブ・メソッドを呼び出す main メソッドも含まれ ています。main メソッドは jPassInt のインスタンスを生成し、pliShowInt() ネイティブ・メソッドを呼び出します。
このサンプル・プログラムは、整数の入力をユーザーに促し、コマンド行からその値を読み込みます。この作業は、図 31 に 示す try/catch ステートメント内で行われます。
// Read an integer, call PL/I, display new integer upon return
import java.io.*;
import java.lang.*;
public class jPassInt{
/* Fields to hold Java string and int */
int myInt;
String myString;
/* Load the PL/I native library */
static {
System.loadLibrary("passInt");
}
/* Declare the PL/I native method */
public native void pliShowInt();
/* Main Java class */
public static void main(String[] arg) {
System.out.println(" ");
/* Instantiate Java class and initilize string */
jPassInt pInt = new jPassInt();
pInt.myInt = 1024;
pInt.myString = " ";
/* Prompt user for an integer */
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
/* Process until 'quit' received */
while (!pInt.myString.equalsIgnoreCase("quit")) {
System.out.println
("From Java: Enter an Integer or 'quit' to quit.");
System.out.print("Java Prompt > ");
/* Get string from command line */
pInt.myString = in.readLine();
if (!pInt.myString.equalsIgnoreCase("quit"))
{
/* Set int to integer value of String */
pInt.myInt = Integer.parseInt( pInt.myString );
/* Call PL/I native method */
pInt.pliShowInt();
/* Return from PL/I and display new string */
System.out.println(" ");
System.out.println
("From Java: Integer set by PL/I is: " + pInt.myInt );
}
}
} catch (IOException e) {
}
}
}