Home : Course Map : Chapter 22 :
JNI Demo - Java Code
JavaTech
Course Map
Chapter 22

Introduction
Java Code
C++ Code
Support Files
Resources

Exercises

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The java demo files for the JNI example are given below.

 

JNIDemo.java

package javatech.jni22;

/** Demonstrates exception handling and calling back into Java from the native
  * code. **/
public class JNIDemo
{
  // Declare native methods.
  public static native void doStatic ();
  public native void doSomething ();

  // Declare static variables.
  private static float a_static_float;

  // Declare instance variables.
  private int some_int;
  private int[] array;
  private int[][] array2d;
  private String some_string;
  private MyCustomObject my_custom;

  // Load the native library.
  static { System.loadLibrary ("NativeJNIDemo"); }

  /** Constructs the JNIDemo object and initializes variables. **/
  public JNIDemo () {
    a_static_float = 4.0f;
    some_int = 2;
    array = new int[5];
    array2d = new int[5][2];
    some_string = "hello JNIDemo";
    my_custom = new MyCustomObject ();
  } //ctor


  /** This is the callback function called from the native code. It throws
   * an exception that is caught in the native code. **/
  private int callback (int x) throws java.io.IOException {
    if (x>0)
      throw new java.io.IOException ("fake io exception from Java");
    return 2*x;
  } // callback


  /** Starts the demo running. **/
  public static void main (String[] args) {
    JNIDemo demo = new JNIDemo ();
    System.out.println ("Before doSeomthing(), demo.my_custom.val = " +
      demo.my_custom.val);
    demo.doSomething ();
    System.out.println ("demo.my_custom.val = " + demo.my_custom.val);
  } // main
} // class JNIDemo


/** Just a simple class to demonstated handling of custom objects on the
* native side. **/
class MyCustomObject {
  int val = 13;
}

JNIHelloWorld.java

package javatech.jni22;

/** Demonstrates perhaps the simplest JNI example imagineable. Calls a native
* method without passing any arguments. **/
public class JNIHelloWorld
{
  // Load the native library.
  static { System.loadLibrary ("NativeHelloWorld"); }

  // Declare the native methods.
  public    native void nativeHelloWorld ();
  public static native void nativeHelloWorldStatic ();

  /** Constructs a JNIHelloWorld object that calls a non-static native
   * method. **/
  public JNIHelloWorld () {
    // Call the native method.
    System.err.println ("JNIHelloWorld.ctor: calling nativeHelloWorld()");
    nativeHelloWorld ();
  } // ctor


  /** Runs the demo. First calls a static native method, then instantiates
   * the constructor to call a non-static method. **/
  public static void main (String[] args) {
    System.err.println ("main: calling nativeHelloWorldStatic()");
    nativeHelloWorldStatic ();
    System.err.println ("main: instantiating JNIHelloWorld");
    new JNIHelloWorld ();
    System.err.println ("main: exiting");
  } // main
} // JNIHelloWorld
StringExample.java
package javatech.jni22;

/** Demonstrates passing a Java string to a native method. **/
public class StringExample
{
  // Load the native library.
  static { System.loadLibrary ("NativeString"); }

  // Declare the native methods.
  public native String nativeProcessString (String s);

  /** Constructs the StringExample object and calls the native method. **/
  public StringExample () {

    // Call the nativeProcessString method.
    String s = "test me";
    String back = nativeProcessString (s);
    System.out.println ("input string was '" + s + "'");
    System.out.println ("output string is '" + back + "'");
  } // ctor


  /** Runs the demo. **/
  public static void main (String[] args) {
    new StringExample ();
  } // main
} // StringExample
ArrayExample.java

package javatech.jni22;

/** Demonstrates array handling. **/
public class ArrayExample
{
  // Load the native library.
  static { System.loadLibrary ("NativeArray"); }

  // Declare the native methods.
  public native float nativeProcessArray (float[] array, int elem);
  public native int nativeProcessArrayRegion (int[] array);
  public native void nativeModifyArrayRegion (int[] array);
  public native void nativeProcess2DArray (int[][] array);

  /** Constructs an ArrayExample object which makes calls into the
   * native code. **/
  public ArrayExample () {

    // Call the nativeProcessArray method to extract the 7th element from
    // and array of floats and to modify the 3rd element.
    float[] array = { 0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f };
    System.out.println ("Calling nativeProcessArray");
    float element7 = nativeProcessArray (array, 7);
    System.out.println ("7th array element is " + element7);
    System.out.println ("modified array element 3 is " + array[3]);

    // Call nativeProcessArrayRegion() to sum elements 5 to 15 of
    // the long int array.
    int[] very_long = new int[1000];
    for (int i=0; i < very_long.length; i++)
      very_long[i] = i;
    System.out.println ("\nCalling nativeProcessArrayRegion");
    int sum = nativeProcessArrayRegion (very_long);
    System.out.println ("sum from native method = " + sum);

    // Call nativeModifyArrayRegion() to modify elements 5 to 15.
    // Print out just the first 20 elements to see what got modified.
    System.out.println ("\nCalling nativeModifyArrayRegion");
    nativeModifyArrayRegion (very_long);
    for (int i=0; i < 20; i++)
      System.out.println ("very_long["+i+"] = " + very_long[i]);

    // Call nativePRocess2DArray.
    int[][] i2d = new int[2][4]; // an array of 2 arrays of int[4]
    System.out.println ("\nlength of i2d  is " + i2d.length);
    System.out.println ("length of i2d[0] is " + i2d[0].length);
    System.out.println ("length of i2d[1] is " + i2d[1].length);
    for (int i=0; i < 2; i++)
      for (int j=0; j<4; j++)
        i2d[i][j] = 1000*(i+1) + (j+1);
    System.out.println ("original values:");
    for (int i=0; i<2; i++)
      for (int j=0; j < 4; j++)
        System.out.println ("i2d["+i+"]["+j+"] = " + i2d[i][j]);
    nativeProcess2DArray (i2d);
    System.out.println ("modified values:");
    for (int i=0; i < 2; i++)
      for (int j=0; j < 4; j++)
        System.out.println ("i2d["+i+"]["+j+"] = " + i2d[i][j]);
  } // ctor

  /** Starts the demo running. **/
  public static void main (String[] args) {
    new ArrayExample ();
  } // main

} // ArrayExample

 

 

Most recent update: Oct. 14, 2005

  
  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.