课程网站建设的财务分析,专建网站,精选聊城做网站的公司,网站建设创建简单了解一下jni
JNI是一个本地编程接口#xff0c;它允许运行在Java虚拟机的Java代码与用其他语言#xff08;如C,C和汇编#xff09;编写的库交互。
jni函数签名
首先看一下java类型对应的jni类型#xff1a;
Java类型符号BooleanZByteBCharCShortSIntILongJFloatFDo…简单了解一下jni
JNI是一个本地编程接口它允许运行在Java虚拟机的Java代码与用其他语言如C,C和汇编编写的库交互。
jni函数签名
首先看一下java类型对应的jni类型
Java类型符号BooleanZByteBCharCShortSIntILongJFloatFDoubleDVoidV数组[ 比如int[] - [I 如果是二维数组 int[][] - [[Iobjects以L开头以;结尾中间是用/ 隔开的包及类名。比如Ljava/lang/String;如果是嵌套类则用$来表示嵌套。
比如
privite native int test(String arg);则它的签名为
(Ljava/lang/String;)I函数参数的传递
基本类型如整型字符等在Java和native之间是采用值传递Java对象采用的是引用传递
关于局部引用的相关内容可以参考之前的文章 JNI内存方面说明以及相关类型手动释放内存
JavaVM和JNIEnv
这两个结构体在jni.h中有定义
#if defined(__cplusplus)
typedef _JNIEnv JNIEnv;
typedef _JavaVM JavaVM;
#else
typedef const struct JNINativeInterface* JNIEnv;
typedef const struct JNIInvokeInterface* JavaVM;
#endif这里也能看出c版本和c版本在使用调用上有一些不同c相当于又包了一层
下边看一下c的版本 JavaVM
struct _JavaVM {const struct JNIInvokeInterface* functions;#if defined(__cplusplus)jint DestroyJavaVM(){ return functions-DestroyJavaVM(this); }jint AttachCurrentThread(JNIEnv** p_env, void* thr_args){ return functions-AttachCurrentThread(this, p_env, thr_args); }jint DetachCurrentThread(){ return functions-DetachCurrentThread(this); }jint GetEnv(void** env, jint version){ return functions-GetEnv(this, env, version); }jint AttachCurrentThreadAsDaemon(JNIEnv** p_env, void* thr_args){ return functions-AttachCurrentThreadAsDaemon(this, p_env, thr_args); }
#endif /*__cplusplus*/
};JNIEnv
struct _JNIEnv {/* do not rename this; it does not seem to be entirely opaque */const struct JNINativeInterface* functions;#if defined(__cplusplus)jint GetVersion(){ return functions-GetVersion(this); }jclass DefineClass(const char *name, jobject loader, const jbyte* buf,jsize bufLen){ return functions-DefineClass(this, name, loader, buf, bufLen); }jclass FindClass(const char* name){ return functions-FindClass(this, name); }......
} 总的来说 JavaVM是java虚拟机环境每个进程有且只有只有一个。 JNIEnv: 是线程上下文环境每个线程只有一个不能跨线程。 我们可以通过JNIEnv来获取一个JavaVM
jint GetJavaVM(JNIEnv *env, JavaVM **vm);// vm用来存放获得的虚拟机的指针的指针。
// return成功返回0失败返回其他。也可以通过JavaVM来获取一个JNIEnv
jint GetEnv(void** env, jint version)上边也提到了JNIEnv是线程绑定的所以通常用全局的JavaVM获取一个当前线程的JNIEnv的时候通常需要绑定到当前线程
char thread_name[128] { 0 };
prctl(PR_GET_NAME, (char *)(thread_name));
JavaVMAttachArgs args;
args.version JNI_VERSION_1_6;
args.name thread_name;
args.group NULL;
gJvm-AttachCurrentThread(pEnv, (void *)(args))同样的也需要在不使用的时候解绑
gJvm-DetachCurrentThread();本地函数的静态加载和动态加载
所谓静态加载就是我们常见的jni函数的声明是Java_包名_类名_方法名(参数...)这样子的Java方法和本地函数之间的映射关系编译器已经帮我们做了。
下边了解一下动态加载
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved);
JNIEXPORT void JNI_OnUnload(JavaVM* vm, void* reserved);JNI_OnLoad方法是在动态库被加载时调用而JNI_OnUnload则是在本地库被卸载时调用。所以这两个函数就是一个本地库最重要的两个生命周期方法。
在JNI_OnLoad()中就可以手动注册本地函数做好对java方法的映射。 一般的可以这个样子
const JNINativeMethod gMethods[] {{native函数名, native函数签名, (void *)native函数}, //第三个为函数指针...//别的native函数
};JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved){JNIEnv *env NULL;jint result -1;if (vm-GetEnv((void **) env, JNI_VERSION_1_6) ! JNI_OK) {return result;}// 动态注册native functionjclass clazz env-FindClass(com.xx.xx.xxclass); env-RegisterNatives(clazz, method, 1) //第三个参数为总的数量// 返回jni的版本return JNI_VERSION_1_6;
}
访问字段和函数
对于jni函数中传入的jobject对象要想访问对象的字段和函数就需要先获取对应的class引用
jobject testObject; //假设我们已经有了这么一个对象jclass testCls env-GetObjectClass(testObject); //获取class引用
//int testFiled;
//int test(String arg);
jfieldID testFiledId env-GetFieldID(testCls, testFiledID, I);
jmethodID testMethodId env-GetMethodID(testCls, test, (Ljava/lang/String;)I);// 注意对字段的get/set,函数的调用都必须使用具体的jobject对象而不能是class引用
env-GetIntField(testObjcet, testFiledId);
env-CallIntMethod(testObject, testMethodId, arg...);java像Native中的对象的传递一般也是安装上边的方式来进行取出字段的值再赋值给native对应的对象
如果是native的对象回调到java层怎么做
// 先找到class引用
jclass jcs env-FindClass(com/xx/xx/xxclass);
// 创建对象有两种方法
// 第一种是调用类的构造函数mthodId就是构造函数的id
jmethodID cls_constructor env-GetMethodID(jcs, init, ()V);jobject NewObject(JNIEnv *env, jclass clazz, jmethodID methodID, ...);// 第二种是直接alloc对象
jobject AllocObject(jclass clazz)//然后将native对象赋值给jobject即可
文章转载自: http://www.morning.lmxrt.cn.gov.cn.lmxrt.cn http://www.morning.fyzsq.cn.gov.cn.fyzsq.cn http://www.morning.jbtwq.cn.gov.cn.jbtwq.cn http://www.morning.tdwjj.cn.gov.cn.tdwjj.cn http://www.morning.pjyrl.cn.gov.cn.pjyrl.cn http://www.morning.xywfz.cn.gov.cn.xywfz.cn http://www.morning.ymyhg.cn.gov.cn.ymyhg.cn http://www.morning.sxmbk.cn.gov.cn.sxmbk.cn http://www.morning.rnfn.cn.gov.cn.rnfn.cn http://www.morning.wqnc.cn.gov.cn.wqnc.cn http://www.morning.smzr.cn.gov.cn.smzr.cn http://www.morning.kyjpg.cn.gov.cn.kyjpg.cn http://www.morning.mfnsn.cn.gov.cn.mfnsn.cn http://www.morning.sjpht.cn.gov.cn.sjpht.cn http://www.morning.lgtzd.cn.gov.cn.lgtzd.cn http://www.morning.mhfbf.cn.gov.cn.mhfbf.cn http://www.morning.ffksr.cn.gov.cn.ffksr.cn http://www.morning.kjkml.cn.gov.cn.kjkml.cn http://www.morning.jmnfh.cn.gov.cn.jmnfh.cn http://www.morning.bfhrj.cn.gov.cn.bfhrj.cn http://www.morning.qrqdr.cn.gov.cn.qrqdr.cn http://www.morning.bqnhh.cn.gov.cn.bqnhh.cn http://www.morning.xnyfn.cn.gov.cn.xnyfn.cn http://www.morning.bpmft.cn.gov.cn.bpmft.cn http://www.morning.ymwcs.cn.gov.cn.ymwcs.cn http://www.morning.madamli.com.gov.cn.madamli.com http://www.morning.lpnpn.cn.gov.cn.lpnpn.cn http://www.morning.jwgnn.cn.gov.cn.jwgnn.cn http://www.morning.ztjhz.cn.gov.cn.ztjhz.cn http://www.morning.pzrpz.cn.gov.cn.pzrpz.cn http://www.morning.hnkkf.cn.gov.cn.hnkkf.cn http://www.morning.nqcts.cn.gov.cn.nqcts.cn http://www.morning.jtdrz.cn.gov.cn.jtdrz.cn http://www.morning.kxxld.cn.gov.cn.kxxld.cn http://www.morning.fglzk.cn.gov.cn.fglzk.cn http://www.morning.bktly.cn.gov.cn.bktly.cn http://www.morning.gmztd.cn.gov.cn.gmztd.cn http://www.morning.yxlhz.cn.gov.cn.yxlhz.cn http://www.morning.zfhwm.cn.gov.cn.zfhwm.cn http://www.morning.lkcqz.cn.gov.cn.lkcqz.cn http://www.morning.knpmj.cn.gov.cn.knpmj.cn http://www.morning.kjfsd.cn.gov.cn.kjfsd.cn http://www.morning.nfcxq.cn.gov.cn.nfcxq.cn http://www.morning.wjxtq.cn.gov.cn.wjxtq.cn http://www.morning.snlxb.cn.gov.cn.snlxb.cn http://www.morning.tpxgm.cn.gov.cn.tpxgm.cn http://www.morning.gppqf.cn.gov.cn.gppqf.cn http://www.morning.gychx.cn.gov.cn.gychx.cn http://www.morning.tqfnf.cn.gov.cn.tqfnf.cn http://www.morning.rqpgk.cn.gov.cn.rqpgk.cn http://www.morning.tqrjj.cn.gov.cn.tqrjj.cn http://www.morning.dycbp.cn.gov.cn.dycbp.cn http://www.morning.qqrqb.cn.gov.cn.qqrqb.cn http://www.morning.gthgf.cn.gov.cn.gthgf.cn http://www.morning.lkgqb.cn.gov.cn.lkgqb.cn http://www.morning.jbhhj.cn.gov.cn.jbhhj.cn http://www.morning.qyhcm.cn.gov.cn.qyhcm.cn http://www.morning.qnsmk.cn.gov.cn.qnsmk.cn http://www.morning.nlhcb.cn.gov.cn.nlhcb.cn http://www.morning.xwlmr.cn.gov.cn.xwlmr.cn http://www.morning.xxrwp.cn.gov.cn.xxrwp.cn http://www.morning.mnyzz.cn.gov.cn.mnyzz.cn http://www.morning.hxwhyjh.com.gov.cn.hxwhyjh.com http://www.morning.mlnby.cn.gov.cn.mlnby.cn http://www.morning.bnbtp.cn.gov.cn.bnbtp.cn http://www.morning.jfnlj.cn.gov.cn.jfnlj.cn http://www.morning.qqhfc.cn.gov.cn.qqhfc.cn http://www.morning.qmnjn.cn.gov.cn.qmnjn.cn http://www.morning.kgrwh.cn.gov.cn.kgrwh.cn http://www.morning.nhdw.cn.gov.cn.nhdw.cn http://www.morning.zgqysw.cn.gov.cn.zgqysw.cn http://www.morning.kfbth.cn.gov.cn.kfbth.cn http://www.morning.npmpn.cn.gov.cn.npmpn.cn http://www.morning.baguiwei.com.gov.cn.baguiwei.com http://www.morning.yjxfj.cn.gov.cn.yjxfj.cn http://www.morning.jcbjy.cn.gov.cn.jcbjy.cn http://www.morning.lxbml.cn.gov.cn.lxbml.cn http://www.morning.mdrnn.cn.gov.cn.mdrnn.cn http://www.morning.ddjp.cn.gov.cn.ddjp.cn http://www.morning.jyyw.cn.gov.cn.jyyw.cn