Home : Course Map : Chapter 22 :
JNI Demo - C++ 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 C++ code for the JNI example are shown below.

 

NativeJNIDemo.cpp

#include "javatech_jni22_JNIDemo.h"

JNIEXPORT void JNICALL
  Java_javatech_jni22_JNIDemo_doStatic (JNIEnv *jenv, jclass cls)
{} // doStatic

JNIEXPORT void JNICALL
  Java_javatech_jni22_JNIDemo_doSomething (JNIEnv *jenv, jobject jo)
{
  // Find the jclass corresponding to the jobject
  jclass cls = jenv->GetObjectClass (jo);

  // Get the static a_static_float field.
  jfieldID fid = jenv->GetStaticFieldID (cls, "a_static_float", "F");
  jfloat a_static_float = jenv->GetStaticFloatField (cls, fid);
  printf ("got a_static_float = %f\n", a_static_float);

  // Find the field ID of the 'my_custom' field
  jfieldID my_custom_fid = jenv->GetFieldID (
    cls, "my_custom", "Ljavatech/jni22/MyCustomObject;"
  );
  if (my_custom_fid == NULL)
    return;
  printf ("got my_custom_fid\n");

  // Get the jobject reference to 'my_custom'
  jobject my_custom_jo = jenv->GetObjectField (jo, my_custom_fid);
  printf ("got my_custom_jo\n");

  // Find the jclass of the my_custom_jo jobject.
  jclass my_custom_cls = jenv->GetObjectClass (my_custom_jo);
  printf ("got my_custom_cls\n");

  // Find the field ID of the 'val' field of MyCustomObject.
  jfieldID val_fid = jenv->GetFieldID (my_custom_cls, "val", "I");
  if (val_fid == NULL) {
    jthrowable jth = jenv->ExceptionOccurred ();
    if (jth) {
      printf ("  there was an exception\n");
      jenv->ExceptionDescribe ();
      jenv->ExceptionClear ();
      printf ("  described and cleared it\n");
    }
    return;
  }
  printf ("got val_fid\n");

  // Get the 'val' field
  jint val = jenv->GetIntField (my_custom_jo, val_fid);
  printf ("got val = %d\n", val);

  // Manipulate it somehow
  jint multiplier = (jint) a_static_float;
  val *= multiplier;
  printf ("multiplied it by %f\n", a_static_float);

  // Set it back into my_custom_jo
  jenv->SetIntField (my_custom_jo, val_fid, val);
  printf ("put it back into my_custom_jo\n");

  // Get the method ID to call a Java method
  jmethodID mid = jenv->GetMethodID (cls, "callback", "(I)I");
  if (mid == NULL)
    return;
  printf ("got mid\n");

  jint param = 8;
  // Call the Java method
  printf ("calling Java 'callback' method\n");
  jint ret = jenv->CallIntMethod (jo, mid, param);
  // Check for any exception that may have occurred during the Java method
  jthrowable jth = jenv->ExceptionOccurred ();
  if (jth) {
    printf ("  there was an exception\n");
    jenv->ExceptionDescribe ();
    jenv->ExceptionClear ();
    printf ("  did not return from Java 'callback'\n");
  }
  else
    printf ("  return from 'callback' = %d\n", ret);
} // doSomething

NativeHelloWorld.cpp
#include "javatech_jni22_JNIHelloWorld.h"
#include <stdio.h>

// This is the non-static native method. Note that it receives a jobject
// argument inthe second position.
JNIEXPORT void JNICALL
  Java_javatech_jni22_JNIHelloWorld_nativeHelloWorld
  (JNIEnv *, jobject)
{
  printf ("   Hello World (non-static)\n");
  return;
}

// The static native method receives a jclass variable in the second position.
JNIEXPORT void JNICALL
  Java_javatech_jni22_JNIHelloWorld_nativeHelloWorldStatic
  (JNIEnv *, jclass)
{
  printf ("   Hello World (static)\n");
  return;
}
NativeString.cpp
#include "javatech_jni22_StringExample.h"
#include <stdio.h>
#include <string.h>

JNIEXPORT jstring JNICALL
  Java_javatech_jni22_StringExample_nativeProcessString
  (JNIEnv *jenv, jobject jo, jstring js)
{
  // Get jstring into C string format.
  const char* cs = jenv->GetStringUTFChars (js, NULL);
  char *cstring = new char [strlen (cs) + 1]; // +1 for null terminator
  sprintf (cstring, "%s", cs);
  jenv->ReleaseStringUTFChars (js, cs);

  // Reverse the order of characters.
  int len = strlen (cstring);
  char *reverse = new char [len+1]; // + 1 for null terminator
  for (int i=0; i < len; i++)
    reverse[i] = cstring[len-i-1];
  reverse[len] = 0; // null terminate

  // Create a new jstring for return.
  jstring jreverse = jenv->NewStringUTF (reverse);
  return jreverse;
} // nativeProcessString
NativeArray.cpp

#include "javatech_jni22_ArrayExample.h"

JNIEXPORT jfloat JNICALL
  Java_javatech_jni22_ArrayExample_nativeProcessArray
  (JNIEnv *jenv, jobject, jfloatArray the_jarray, jint j_index)
{
  float* c_array = jenv->GetFloatArrayElements (the_jarray, NULL);
  c_array[3] = -999.f;
  int c_index = j_index;
  float element = c_array[c_index];
  jenv->ReleaseFloatArrayElements (the_jarray, c_array, 0);
  jfloat jelement = element;
  return jelement;
} // nativeProcessArray

JNIEXPORT jint JNICALL
  Java_javatech_jni22_ArrayExample_nativeProcessArrayRegion
  (JNIEnv *jenv, jobject, jintArray the_jarray)
{
  jint region[10];
  jint sum = 0;
  jenv->GetIntArrayRegion (the_jarray, 5, 10, region);
  for (int i=0; i < 10; i++)
    sum += region[i];
  return sum;
} // nativeProcessArrayRegion

JNIEXPORT void JNICALL
  Java_javatech_jni22_ArrayExample_nativeModifyArrayRegion
  (JNIEnv *jenv, jobject, jintArray the_jarray)
{
  jint region[10];
  for (int i=0; i < 10; i++)
    region[i] = -5 - i;
  jenv->SetIntArrayRegion (the_jarray, 5, 10, region);
} // nativeProcessArrayRegion

JNIEXPORT void JNICALL
  Java_javatech_jni22_ArrayExample_nativeProcess2DArray
  (JNIEnv *jenv, jobject, jobjectArray joa2d)
{
  // Get the length of the 2D array
  jint len1 = jenv->GetArrayLength (joa2d);
  // Extract the 0-th element from the 2D array and cast into a jintArray
  jintArray i2d_0 = (jintArray) jenv->GetObjectArrayElement (joa2d, 0);
  // Get the length of the extracted 1D array
  jint len2 = jenv->GetArrayLength (i2d_0);

  // Allocate a buffer to receive a copy of the elements from the 1D array
  jint buffer[4];
  // Copy the all of the elements of the entire 1D array into the buffer
  jenv->GetIntArrayRegion (i2d_0, 0, 4, buffer);
  // Modify them in some way.
  for (int i=0; i < 4; i++)
    buffer[i]*=2;
  // Insert the modified buffer back into the 1D array.
  jenv->SetIntArrayRegion (i2d_0, 0, 4, buffer);
} // nativeProcessArrayRegion

 

 

 

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.