一、javah头文件
Java通过JNI机制调用c/c++写的native程序。c/c++开发的native程序需要遵循一定的JNI规范,下面的例子就是一个JNI函数声明:
JNIEXPORT jint JNICALL Java_jnitest_MyTest_test
(JNIEnv * env, jobject obj, jint arg0);
JVM负责从Java Stack转入C/C++ Native Stack。当Java进入JNI调用,除了函数本身的参数(arg0),会多出两个参数:JNIEnv指针和jobject指针。JNIEnv指针是JVM创建的,用于Native的c/c++方法操纵Java执行栈中的数据,比如Java Class, Java Method等。
二、Jni数据类型
数据类型如下,记得在Get***之后都要调用Release***。否则会导致内存泄露。
Native Code Type | Functions used |
---|---|
jboolean | NewBooleanArray |
GetBooleanArrayElements | |
GetBooleanArrayRegion/SetBooleanArrayRegion | |
ReleaseBooleanArrayElements | |
jbyte | NewByteArray |
GetByteArrayElements | |
GetByteArrayRegion/SetByteArrayRegion | |
ReleaseByteArrayElements | |
jchar | NewCharArray |
GetCharArrayElements | |
GetCharArrayRegion/SetCharArrayRegion | |
ReleaseCharArrayElements | |
jdouble | NewDoubleArray |
GetDoubleArrayElements | |
GetDoubleArrayRegion/SetDoubleArrayRegion | |
ReleaseDoubleArrayElements | |
jfloat | NewFloatArray |
GetFloatArrayElements | |
GetFloatArrayRegion/SetFloatArrayRegion | |
ReleaseFloatArrayElements | |
jint | NewIntArray |
GetIntArrayElements | |
GetIntArrayRegion/SetIntArrayRegion | |
ReleaseIntArrayElements | |
jlong | NewLongArray |
GetLongArrayElements | |
GetLongArrayRegion/SetLongArrayRegion | |
ReleaseLongArrayElements | |
jobject | NewObjectArray |
GetObjectArrayElement/SetObjectArrayElement | |
jshort | NewShortArray |
GetShortArrayElements | |
GetShortArrayRegion/SetShortArrayRegion | |
ReleaseShortArrayElements |
三、jbyte使用注意
If you are using GetByteArrayElements you have to call ReleaseByteArrayElements after you are done with the array in JNI, because the JVM will prevent the freeing of this array in java until you do so.Please post the code to get a clearer idea.Something like:
boolean isCopy;
jbyte* b = GetByteArrayElements(env, arr, &isCopy);
You should be able to cast b to char* at this point in order to access the data in the array. Note that this may create a copy of the data, so you'll want to make sure to release the memory using ReleaseByteArrayElements:
ReleaseByteArrayElements(env, arr, b, 0);
The last parameter is a mode indicating how changes to b should be handled.
The last argument to the ReleaseByteArrayElements function above can have the following values:
0: Updates to the array from within the C code are reflected in the Java language copy(indicates that the values are copied back to arr).
JNI_COMMIT: The Java language copy is updated, but the local jbyteArray is not freed.
JNI_ABORT: Changes are not copied back, but the jbyteArray is freed. The value is used only if the array is obtained with a get mode of JNI_TRUE meaning the array is a copy.(If you don't want to copy the data back to arr)
小心最后一个参数,如果为0是会释放 b 所指向的内存的. 如果b刚好指向一个栈上的数组的话,这样可能在Release 版本中造成内存方面的随机错误.可以用JNI_COMMIT来避免.
四、void *、long *问题
You can use jlong to pass a pointer (or a pointer to pointer, or whatever) back to Java. Java code won't be able to use it for anything, other than passing it as an argument to one of your other methods; but often that's all you really want.
If, on the other hand, you want Initialize() to be called with data set up in Java, then void * isn't appropriate; you'll need to use a Java class, and use reflection in JNI to get the information you need out of it.
Crudely, you could wrap malloc() and free():
jlong Java_c_utils_malloc(JNIEnv* env, jclass clazz, jint size) {
return (jlong) malloc(size);
}
void Java_c_utils_free(JNIEnv* env, jclass clazz, jlong ptr) {
free((void *) ptr);
}
and then use them (to no effect!) in Java:
long ptr = utils.malloc(100);
// Store ptr for a while
utils.free(ptr);
五、将C++中char *的buffer传递给Java
char*如果是一般的字符串的话,作为string传回去就可以了。
如果是含有’\0’的buffer,最好作为bytearray传出,因为可以制定copy的length,如果copy到string,可能到’\0’就截断了。有以下两种方式传递得到的数据:
(1): 在jni中直接new一个byte数组,然后调用函数
(*env)->SetByteArrayRegion(env, bytearray, 0, len, buffer);
将buffer的值copy到bytearray中,函数直接return bytearray。但是此方法容易产生内存泄露问题。
(2):return错误号,数据作为参数传出。
参考文档:
http://java.sun.com/docs/books/jni/html/jniTOC.html
http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jnistring.html
http://docs.oracle.com/javase/1.4.2/docs/guide/jni/spec/functions.html