publicclassHelloJNI{ // Save as HelloJNI.java static { System.loadLibrary("hello"); // Load native library hello.dll (Windows) or libhello.so (Unixes) // at runtime // This library contains a native method called sayHello() } // Declare an instance native method sayHello() which receives no parameter and returns void privatenativevoidsayHello(); // Test Driver publicstaticvoidmain(String[] args){ new HelloJNI().sayHello(); // Create an instance and invoke the native method } }
JNIEXPORT jstring JNICALL Java_HelloJNI_sayHelloString(JNIEnv *env, jobject thisObj, jstring inJNIStr){ constchar *inCStr = (*env)->GetStringUTFChars(env, inJNIStr, NULL); if (NULL == inCStr) returnNULL; // Step 2: Perform its intended operations printf("In C, the received string is: %s\n", inCStr); (*env)->ReleaseStringUTFChars(env, inJNIStr, inCStr); // release resources // Prompt user for a C-string char outCStr[128]; printf("Enter a String: "); scanf("%s", outCStr); // not more than 127 characters // Step 3: Convert the C-string (char*) into JNI String (jstring) and return return (*env)->NewStringUTF(env, outCStr); }
object 相关函数
主要是通过反射来读写 object 对象的field
1 2 3 4 5 6 7 8
jclass GetObjectClass(JNIEnv *env, jobject obj); // Returns the class of an object. jfieldID GetFieldID(JNIEnv *env, jclass cls, constchar *name, constchar *sig); // Returns the field ID for an instance variable of a class. NativeType Get<type>Field(JNIEnv *env, jobject obj, jfieldID fieldID); void Set<type>Field(JNIEnv *env, jobject obj, jfieldID fieldID, NativeType value);
JNIEXPORT void JNICALL Java_TestJNIInstanceVariable_modifyInstanceVariable (JNIEnv *env, jobject thisObj){ // Get a reference to this object's class jclass thisClass = (*env)->GetObjectClass(env, thisObj); // int // Get the Field ID of the instance variables "number" jfieldID fidNumber = (*env)->GetFieldID(env, thisClass, "number", "I"); if (NULL == fidNumber) return; // Get the int given the Field ID jint number = (*env)->GetIntField(env, thisObj, fidNumber); printf("In C, the int is %d\n", number); // Change the variable number = 99; (*env)->SetIntField(env, thisObj, fidNumber, number); // Get the Field ID of the instance variables "message" jfieldID fidMessage = (*env)->GetFieldID(env, thisClass, "message", "Ljava/lang/String;"); if (NULL == fidMessage) return; // String // Get the object given the Field ID jstring message = (*env)->GetObjectField(env, thisObj, fidMessage); // Create a C-string with the JNI String constchar *cStr = (*env)->GetStringUTFChars(env, message, NULL); if (NULL == cStr) return; printf("In C, the string is %s\n", cStr); (*env)->ReleaseStringUTFChars(env, message, cStr); // Create a new C-string and assign to the JNI string message = (*env)->NewStringUTF(env, "Hello from C"); if (NULL == message) return; // modify the instance variables (*env)->SetObjectField(env, thisObj, fidMessage, message); }
所有基本类型的 数组类型: 数组容器 也需要手动释放
包括以下类型
jintArray, jbyteArray, jshortArray, jlongArray, jfloatArray, jdoubleArray, jcharArray and jbooleanArray