2008年2月17日 星期日

JNI

檔名:
.jnilib : mac os x , prefixed by lib and suffixed with .jnilib
.so : solaris and linux
.dll: windows

in library file:
(1) include
(2) function格式:
ex: JNIEXPORT jint JNICALL Java_Test_show(JNIEnv *env, jobject rkObject)
function name = Java_Test_show, 以Java開頭,接著是溝通的Java的class name,最後才是function name
如果Java是在某個package上,要包含package name, 例如class Dog在package Animal下,
則完整的function name是Java_Animal_Dog_eat()
JNIEXPORT and JNICALL make this function to be found
(3)compile:
gcc -c -I /System/Library/Frameworks/JavaVM.framework/Headers test.c

如果是在xcode,在target info panel下的 build tab下的Header Search Paths設成/System/Library/Frameworks/JavaVM.framework/Headers
(4) product jnilib file
gcc -dynamiclib -o libtest.jnilib test.o -framework JavaVM


in Java
(1) check path, jnilib檔的位置要在path上
System.out.println( System.getProperty( "java.library.path" ) );
(2) loads the library named libhello.jnilib
System.loadLibrary("hello")
(3) 宣告要呼叫的library function
ex: public native void show();
以public native開頭


把Array 的結果copy back to 呼叫端
(*env)->ReleaseIntArrayElements(env, ctrllist, jBody, 0);

沒有留言: