Learn Java

Header Files

Observe

Compare the java files and generated header file.

  • Java
          public class Header{
              public static native void a();
              public native void b();
              public static native int c();
              public native void d(int para);
              public native void e(String para);
          }
    
  • Header file

      JNIEXPORT void JNICALL Java_Header_a
        (JNIEnv *, jclass);
    
      JNIEXPORT void JNICALL Java_Header_b
        (JNIEnv *, jobject);
    
      JNIEXPORT jint JNICALL Java_Header_c
        (JNIEnv *, jclass);
    
      JNIEXPORT void JNICALL Java_Header_d
        (JNIEnv *, jobject, jint);
    
      JNIEXPORT void JNICALL Java_Header_e
       (JNIEnv *, jobject, jstring);
    
  • We find all kind of fuctions in Java is mapped to the JNIEnv * argument in header file.

  • The second argument depends on whether the function is static or member function.
  • Primary type is mapped to jxxx type.
  • Reference type is mapped to opaque references, C pointer types that refer to internal data structures in the JVM.

Conclusion

  • JNIEnv *:

    a pointer that points to another pointer pointing to a function table (array of pointer). Each entry in this function table points to a JNI function. These are the functions we are going to use for type conversion

  • jclass -> static function : refering to the class in which the method is define.
  • jobject -> member function : refering to the object on which the method is invoked.