1 / 19

Java Native Interface (JNI)

Java Native Interface (JNI). luux. 1. Introduction(Type). Basic type typedef long jint typedef long long jlong typedef signed char jbyte typedef unsigned char jboolean typedef unsigned short jchar typedef short jshort typedef float jfloat typedef double jdouble

saeran
Download Presentation

Java Native Interface (JNI)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Native Interface (JNI) luux

  2. 1. Introduction(Type) • Basic type • typedef long jint • typedef long long jlong • typedef signed char jbyte • typedef unsigned char jboolean • typedef unsigned short jchar • typedef short jshort • typedef float jfloat • typedef double jdouble • typedef jint jsize

  3. 2) Java Object Reference Type • typedef jobject jclass • typedef jobject jthrowable • typedef jobject jstring • typedef jobject jarray • typedef jarray jbooleanArray • typedef jarray jbyteArray • typedef jarray jcharArray • typedef jarray jshortArray • typedef jarray jintArray • typedef jarray jlongArray • typedef jarray jfloatArray • typedef jarray jdoubleArray • typedef jarray jobjectArray

  4. 3)Others • typedef union jvalue { jboolean z; jbyte b; jchar c; jshort s; jint i; jlong j; jfloat f; jdouble d; jobject l; } jvalue;

  5. 2.Introduction(Structure) • typedef const struct JNINativeInterface_ *JNIEnv; struct JNINativeInterface_ { …//native method }; Method: e.g: jclass (JNICALL *FindClass) (JNIEnv *env, const char *name); jclass (JNICALL *GetSuperclass)(JNIEnv *env, jclass sub); jboolean (JNICALL *IsAssignableFrom)(JNIEnv *env, jclass sub, jclass sup); jint (JNICALL *Throw)(JNIEnv *env, jthrowable obj); jint (JNICALL *ThrowNew)(JNIEnv *env, jclass clazz, const char *msg); jthrowable (JNICALL *ExceptionOccurred)(JNIEnv *env); void (JNICALL *ExceptionDescribe)(JNIEnv *env); void (JNICALL *ExceptionClear)(JNIEnv *env); void (JNICALL *FatalError)(JNIEnv *env, const char *msg);

  6. jobject (JNICALL *NewGlobalRef)(JNIEnv *env, jobject lobj); void (JNICALL *DeleteGlobalRef)(JNIEnv *env, jobject gref); void (JNICALL *DeleteLocalRef)(JNIEnv *env, jobject obj); jboolean (JNICALL *IsSameObject)(JNIEnv *env, jobject obj1, jobject obj2); jobject (JNICALL *NewLocalRef)(JNIEnv *env, jobject ref); jint (JNICALL *EnsureLocalCapacity)(JNIEnv *env, jint capacity); jobject (JNICALL *AllocObject)(JNIEnv *env, jclass clazz); jobject (JNICALL *NewObject)(JNIEnv *env, jclass clazz, jmethodID methodID, ...); jobject (JNICALL *NewObjectV)(JNIEnv *env, jclass clazz, jmethodID methodID, va_list args); jobject (JNICALL *NewObjectA)(JNIEnv *env, jclass clazz, jmethodID methodID, const jvalue *args); jclass (JNICALL *GetObjectClass)(JNIEnv *env, jobject obj); jboolean (JNICALL *IsInstanceOf)(JNIEnv *env, jobject obj, jclass clazz); jmethodID (JNICALL *GetMethodID)(JNIEnv *env, jclass clazz, const char *name, const char *sig); XXX (JNICALL *CallXXXMethod)(JNIEnv *env, jobject obj, jmethodID methodID, ...); XXX (JNICALL *CallXXXMethodV)(JNIEnv *env, jobject obj, jmethodID methodID, va_list args); XXX (JNICALL *CallXXXMethodA)(JNIEnv *env, jobject obj, jmethodID methodID, const jvalue * args);

  7. XXX (JNICALL *CallNonvirtualXXXMethod)(JNIEnv *env, jobject obj, jclass clazz, jmethodID methodID, ...); XXX (JNICALL *CallNonvirtualXXXMethodV)(JNIEnv *env, jobject obj, jclass clazz, jmethodID methodID, va_list args); XXX (JNICALL *CallNonvirtualXXXMethodA)(JNIEnv *env, jobject obj, jclass clazz, jmethodID methodID, const jvalue * args); jfieldID (JNICALL *GetFieldID)(JNIEnv *env, jclass clazz, const char *name, const char *sig); XXX (JNICALL *GetXXXField)(JNIEnv *env, jobject obj, jfieldID fieldID); void (JNICALL *SetXXXField)(JNIEnv *env, jobject obj, jfieldID fieldID, XXX val); jmethodID (JNICALL *GetStaticMethodID)(JNIEnv *env, jclass clazz, const char *name, const char *sig); XXX (JNICALL *CallStaticXXXMethod)(JNIEnv *env, jclass clazz, jmethodID methodID, ...); XXX (JNICALL *CallStaticXXXMethodV)(JNIEnv *env, jclass clazz, jmethodID methodID, va_list args); XXX (JNICALL *CallStaticXXXMethodA) (JNIEnv *env, jclass clazz, jmethodID methodID, const jvalue *args);

  8. jfieldID (JNICALL *GetStaticFieldID)(JNIEnv *env, jclass clazz, const char *name, const char *sig); XXX (JNICALL *GetStaticXXXField) (JNIEnv *env, jclass clazz, jfieldID fieldID); void (JNICALL *SetStaticXXXField)(JNIEnv *env, jclass clazz, jfieldID fieldID, XXX value); jstring (JNICALL *NewString)(JNIEnv *env, const jchar *unicode, jsize len); jsize (JNICALL *GetStringLength)(JNIEnv *env, jstring str); const jchar *(JNICALL *GetStringChars)(JNIEnv *env, jstring str, jboolean *isCopy); void (JNICALL *ReleaseStringChars)(JNIEnv *env, jstring str, const jchar *chars); jstring (JNICALL *NewStringUTF)(JNIEnv *env, const char *utf); jsize (JNICALL *GetStringUTFLength)(JNIEnv *env, jstring str); const char* (JNICALL *GetStringUTFChars)(JNIEnv *env, jstring str, jboolean *isCopy); void (JNICALL *ReleaseStringUTFChars)(JNIEnv *env, jstring str, const char* chars);

  9. jsize (JNICALL *GetArrayLength)(JNIEnv *env, jarray array); XXXArray (JNICALL *NewXXXArray) (JNIEnv *env, jsize len, jclass clazz, XXX init); XXX (JNICALL *GetXXXArrayElement)(JNIEnv *env, XXXArray array, jsize index); void (JNICALL *SetXXXArrayElement)(JNIEnv *env,XXXArray array, jsize index, XXX val); void (JNICALL *ReleaseXXXArrayElements)(JNIEnv *env, XXXArray array, XXX *elems, jint mode); void (JNICALL *GetXXXArrayRegion)(JNIEnv *env, XXXArray array, jsize start, jsize l, XXX *buf); void (JNICALL *SetXXXArrayRegion)(JNIEnv *env,XXXArray array, jsize start, jsize l, const XXX *buf); jint (JNICALL *RegisterNatives)(JNIEnv *env, jclass clazz, const JNINativeMethod *methods, jint nMethods); jint (JNICALL *UnregisterNatives)(JNIEnv *env, jclass clazz);

  10. jint (JNICALL *MonitorEnter)(JNIEnv *env, jobject obj); jint (JNICALL *MonitorExit)(JNIEnv *env, jobject obj); jint (JNICALL *GetJavaVM)(JNIEnv *env, JavaVM **vm); void (JNICALL *GetStringRegion) (JNIEnv *env, jstring str, jsize start, jsize len, jchar *buf); void (JNICALL *GetStringUTFRegion)(JNIEnv *env, jstring str, jsize start, jsize len, char *buf); const jchar * (JNICALL *GetStringCritical)(JNIEnv *env, jstring string, jboolean *isCopy); void (JNICALL *ReleaseStringCritical)(JNIEnv *env, jstring string, const jchar *cstring); jboolean (JNICALL *ExceptionCheck)(JNIEnv *env);

  11. typedef struct JavaVMOption { • char *optionString; • void *extraInfo; • } JavaVMOption; • typedef struct JavaVMInitArgs { • jint version; • jint nOptions; • JavaVMOption *options; • jboolean ignoreUnrecognized; • } JavaVMInitArgs; • typedef struct JavaVMAttachArgs { • jint version; • char *name; • jobject group; • } JavaVMAttachArgs;

  12. typedef struct JDK1_1InitArgs { • jint version; • char **properties; • jint checkSource; • jint nativeStackSize; • jint javaStackSize; • jint minHeapSize; • jint maxHeapSize; • jint verifyMode; • char *classpath; • jint (JNICALL *vfprintf)(FILE *fp, const char *format, va_list args); • void (JNICALL *exit)(jint code); • void (JNICALL *abort)(void); • jint enableClassGC; • jint enableVerboseGC; • jint disableAsyncGC; • jint verbose; • jboolean debugging; • jint debugPort; • } JDK1_1InitArgs;

  13. struct JNIInvokeInterface_ { • void *reserved0; • void *reserved1; • void *reserved2; • jint (JNICALL *DestroyJavaVM)(JavaVM *vm); • jint (JNICALL *AttachCurrentThread)(JavaVM *vm, void **penv, void *args); • jint (JNICALL *DetachCurrentThread)(JavaVM *vm); • jint (JNICALL *GetEnv)(JavaVM *vm, void **penv, jint version); • jint (JNICALL *AttachCurrentThreadAsDaemon)(JavaVM *vm, void **penv, void *args); • };

  14. 3.Introduction(Function) • _JNI_IMPORT_OR_EXPORT_ jint JNICALL • JNI_GetDefaultJavaVMInitArgs(void *args); • _JNI_IMPORT_OR_EXPORT_ jint JNICALL • JNI_CreateJavaVM(JavaVM **pvm, void **penv, void *args); • _JNI_IMPORT_OR_EXPORT_ jint JNICALL • JNI_GetCreatedJavaVMs(JavaVM **, jsize, jsize *); • JNIEXPORT jint JNICALL • JNI_OnLoad(JavaVM *vm, void *reserved); • JNIEXPORT void JNICALL • JNI_OnUnload(JavaVM *vm, void *reserved);

  15. CallNativeMethod • Java code: class HelloWorld { private native void print(); puclic string getString(int i) { return “the string:” + Integer.toString(i); } public static void main(String[] args) { new HelloWorld().print(); } Static { System.loadLibrary("HelloWorld"); } } Javac HelloWorld.java Javah Helloworld Helloword.h: JNIEXPORT void JNICALL Java_HelloWorld_print (JNIEnv *, jobject);

  16. Helloworld.c: • #include <jni.h> • #include <stdio.h> • #include "HelloWorld.h" • JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *env, jobject obj) • { • printf("Hello World!\n"); • Jclass clz = (*env)->GetObjectClass(env, obj); • if ((*env)->ExceptionOccurred(env)) { • (*env)->ExceptionDescribe(env); • } • jmethodID meth = (*env)->GetMethodID(env, clz, “getString”, “(I)Ljava/lang/String;”) • if ((*env)->ExceptionOccurred(env)) { • (*env)->ExceptionDescribe(env); • } • Jstring str = (*env)->CallObjectMethod(env, jobj, meth, 100); • if ((*env)->ExceptionOccurred(env)) { • (*env)->ExceptionDescribe(env); • } • Char *s = (!str) ? NULL : (*env)->GetStringUTFChar(env, str, NULL); • Printf(“ the method return:%s\n”, s); • (*env)->ReleaseStringUTFChars(env, str, s); } • gcc -Ipath HelloWorld.c -shared -o libHelloWorld.so • export LD_LIBRARY_PATH=“path” • Java –Djava.library.path=“path”

  17. Create Java VM by JNI • #include <jni.h> • main() { • JNIEnv *env; • JavaVM *jvm; • JavaVMInitArgs vm_args; • JavaVMOption options[3]; • options[0].optionString = "-Djava.class.path=PATH"; • options[1].optionString = "-Djava.env.path=PATH"; • options[2].optionString = "-verbose:jni"; • vm_args.version = JNI_VERSION_1_4; • vm_args.options = options; • vm_args.nOptions = 3; • vm_args.ignoreUnrecognized = JNI_TRUE; • Int res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); • if (res < 0) { • fprintf(stderr, "Can't create Java VM\n"); exit(1); • } • Jclass cls = (*env)->FindClass(env, “HelloWorld"); • if (cls == NULL) { (*jvm)->DestroyJavaVM(jvm); exit(1); } • jmethodID mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V");

  18. Jstring jstr = (*env)->NewStringUTF(env, " from C!"); • if (jstr == NULL) { • goto destroy; • } • jclass stringClass = (*env)->FindClass(env, "java/lang/String"); • jobjectArray args = (*env)->NewObjectArray(env, 1, stringClass, jstr); • if (args == NULL) { • goto destroy; • } • (*env)->CallStaticVoidMethod(env, cls, mid, args); • destroy: • if ((*env)->ExceptionOccurred(env)) { • (*env)->ExceptionDescribe(env); • } • (*jvm)->DestroyJavaVM(jvm); • }

  19. void thread_fun(JavaVM *jvm) • { • jint res; jclass cls; jmethodID mid; jstring jstr; jclass stringClass; jobjectArray args; JNIEnv *env; char buf[100]; • res = (*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL); • if(res < 0) { fprintf(stderr, "Attach failed\n"); return; } • cls = (*env)->FindClass(env, “HelloWorld"); • if (cls == NULL) { goto detach; } • mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V"); • if (mid == NULL) { goto detach; } • jstr = (*env)->NewStringUTF(env, buf); • if (jstr == NULL) { goto detach; } • stringClass = (*env)->FindClass(env, "java/lang/String"); • args = (*env)->NewObjectArray(env, 1, stringClass, jstr); • if (args == NULL) { goto detach; } • (*env)->CallStaticVoidMethod(env, cls, mid, args); • detach: • if ((*env)->ExceptionOccurred(env)) { (*env)->ExceptionDescribe(env); } • (*jvm)->DetachCurrentThread(jvm); }

More Related