Step 1: Writing the Java program
Procedure
- Declare the native method.
public native void pliShowInt(); - Load the native library.
static { System.loadLibrary("passInt"); } - Write the Java™ Main
method.
The
jPassIntclass also includes amainmethod to instantiate the class and call the native method. Themainmethod instantiatesjPassIntand calls thepliShowInt()native method.This sample program prompts the user for an integer and reads that value in from the command line. This is done within a
try/catchstatement as shown in javcod3.html#javcod3__jsamp3.Java sample program #3 - Passing an integer// 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 initialize 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) { } } }