文章标题

来源:互联网 发布:小学生近视数据 编辑:程序博客网 时间:2024/05/21 10:36

JNI

开发步骤:

  1. 新建工程,定义native方法,

  2. 通过javah命令生成.h文件

  3. 将生成的.h文件复制到c项目中

  4. 导入jni支持头文件(jni.h,jni_md.h)以上头文件在jdk安装目录下

  5. 实现native方法

  6. 配置属性,生成(window:.dll Linux:.so),配置环境变量:将动态库所在目录配置到path,,并完成java中的引入操作

  7. 重启eclipse

  8. 运行程序

声明native方法

public native String stringFromJni();

load the shared Libraries(加载动态库)

static{    System.loadLibrary("hello-jni");}

java中有2种方法:instance method(实例方法) and static method(静态方法)

instance method 时,Native instance method get the instance reference as their second parameter as a jobject value.as shown below.
使用实例方法中调用native方法时,native方法将调用java实例方法的对象的引用作为第二个参数,使用jobject表示

JNIEXPORT jstring JNICALL Java_com_example_hellojni_HelloJni_stringFromJNI (  JNIEnv * env,jobject thiz);

由于静态方法不需要绑定对象,Native methods get the class reference as a jclass value,as shown below.
使用静态方法调用native方法时,native方法将拥有静态方法的类作为第二个参数,也就是jclass

JNIEXPORT jstring JNICALL Java_com_example_hellojni_HelloJni_stringFromJNI (  JNIEnv * env,jclass clazz);

java中有2种类型的数据:Primitive(原始) types and Reference types.

Primitive types are directly mapped to c/c++ equivalents.The jni uses type definitions to make this mapping transparent to developers.
原始类型直接对应着c/c++,为了映射关系更加简单jni定义了一套类型与java对应

java jni Boolean Jboolean Byte Jbyte Char Jchar Short Jshort Long Jlong Float Jfloat String jstring Object[] jobjectArray boolean[] jbooleanArray byte[] jbyteArray char[] jcharArray short[] jshortArray int[] jintArray long[] jlongArray float[] jfloatArray double[] jdoubleArray other arrays Jarray

*Reference* types are passed as opaque(不透明) references to the native code rather than native data types,and they cannot be consumed and modified directly.JNI provides a set of APIs for interacting with these reference types.These APIs are provided to native function through the JNIEnv interface pointer.next step ,we will briefly explore these APIs pertinent(有关的) to the following types and components:
以上大体意思:jni和java中的类型不能直接转换,jni提供了一套API供开发者进行类型转换
Strings,Arrays,NIO Buffers,Fields,Methods

String Operations(字符串的操作)

Java strings are handled by the JNI as reference types. These reference types are not directly usable as native C strings.JNI provides the necessary functions to convert these java string reference to C strings and back.Since java string objects are immutable ,JNI does not provide any function to modify the content of an existing java string.****JNI supports both Unicode and UTF-8 encoded strings,and it provides two sets of functions through the JNIEnv interface pointer to handle each of these string encodings.

*New String : convert to java string一下代码是将c中得字符串转位java中的字符串*

New string instances can be constructed from the native code by using the functions NewString for Unicode strings and NewStringUTF for UTF-8 strings.these functions take a c string and returns a java string reference type,a jstring value.

jstring javaString;javaString = (*env)->NewStringUTF(env,"hello");

*convert to c string (将java中的字符串转位c中的字符串)*

Java strings can be converted to c strings using the functions GetStringChars for Unicode strings and GetStringUTFChars for UTF-8 strings.

const jbyte* str;jboolean isCopy;str = (*env)->GetStringChars(env,javaString,&isCopy);
0 0