JNI学习小结

Java中类型与C/C++中对应关系
JNI学习小结
 
Java中的类的对应
 
JNI学习小结
Sign签名, 用来识别对应各个方法。
JDK下的javap.exe能输出签名。用法javap -s -p 完整类名
 
JNI学习小结
 

  下面是几个例子程序:

1、C++本地方法中获取Java中的变量以及调用Java中的方法

  Java代码:

 package com.test;

 import java.util.Date;

 public class TestNative {
public native void sayHello();
private int a = 10;
public int function(int x,Date date,int[] y){
System.out.println("function");
return 0;
}
public double max(double a,double b){
return a>b?a:b;
}
/**
* @param args
*/
public static void main(String[] args) {
System.loadLibrary("NativeCode");
TestNative tNative = new TestNative();
tNative.sayHello();
} }

  C++本地代码:

  com_test_TestNative.h代码省略了

 #include<iostream.h>
#include"com_test_TestNative.h" JNIEXPORT void JNICALL Java_com_test_TestNative_sayHello(JNIEnv *env, jobject obj)
{
//因为sayHello不是静态函数,所以传进来的obj就是调用这个函数的对象,否则就是传入native方法所在的类
jclass hello_clazz = env->GetObjectClass(obj); //得到的就是native方法所在的类
// jfieldID fieldId_a = env->GetFieldID(hello_clazz,"a","I");
// jmethodID methodId_fun = env->GetMethodID(hello_clazz,"function","(ILjava/util/Date;[I)I");
// env->CallIntMethod(obj,methodId_fun,0L,NULL,NULL); //方法调用
// cout<<"hello world!"<<endl;
// cout<<"successful"<<endl;
/*
jfieldID field_a = env->GetFieldID(hello_clazz,"a","I"); //得到字段a的ID
jint a = env->GetIntField(obj,field_a); //得到字段a的值
cout<<a<<endl;
env->SetIntField(obj,field_a,100L);
*/
jmethodID methodId_max = env->GetMethodID(hello_clazz,"max","(DD)D");
jvalue *values = new jvalue[]; //jvalue是用来向java函数中传参数的
values[].d = 3.14;
values[].d = 3.22;
//jdouble max = env->CallDoubleMethod(obj,methodId_max,3.18,3.15); //第一种方法调用
jdouble max = env->CallDoubleMethodA(obj,methodId_max,values); //第二种方法调用
delete [] values;
cout<<max<<endl; }

2、C++本地方法中对Java中的字符串操作(实现用户输入一个字符串,在C++中对其反转)

  Java代码:

 package com.test;

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; public class TestStr {
public native void cppCode();
private String message;
public static void main(String[] args) throws IOException {
System.loadLibrary("CPP");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str = reader.readLine();
TestStr obj = new TestStr();
obj.message = str;
obj.cppCode();
System.out.println("java :"+obj.message);
} }

  c++本地方法:

  com_test_TestStr.h省略了

 #include "com_test_TestStr.h"
#include<windows.h>
#include<string>
#include<algorithm>
using namespace std;
JNIEXPORT void JNICALL Java_com_test_TestStr_cppCode(JNIEnv *env, jobject obj)
{
jfieldID fid_msg = env->GetFieldID(env->GetObjectClass(obj),"message","Ljava/lang/String;");
jstring j_msg = (jstring)env->GetObjectField(obj,fid_msg);
jsize len = env->GetStringLength(j_msg); //得到字符串的长度
jchar* jstr = new jchar[len+]; //申请空间
jstr[len] = L'\0';
env->GetStringRegion(j_msg,,len,jstr);
//env->ReleaseStringChars(j_msg,jstr);
//MessageBoxW(NULL,(const wchar_t*)jstr,L"Title",MB_OK);
wstring wstr((const wchar_t*)jstr);
delete[] jstr; //释放空间
std::reverse(wstr.begin(),wstr.end()); //字符串反转
jstring j_new_str = env->NewString((const jchar*)wstr.c_str(),(jint)wstr.size()); //创建一个新的字符串
env->SetObjectField(obj,fid_msg,j_new_str); //设置字符串给java对象 }

3、C++本地方法中获取Java中的数组(实现用C++本地方法将Java中的数组排序)

  Java代码:

 package com.test;

 public class TestArray {

     public int[] array = {,,,,,,,,,};

     public native void callCppFun();

     public static void main(String[] args) {
System.loadLibrary("TestArray");
TestArray obj = new TestArray();
obj.callCppFun();
for(int a:obj.array){
System.out.println(a);
}
} }

  C++本地代码:

  com_test_TestArray.h省略了

 #include"com_test_TestArray.h"
#include<iostream>
#include<algorithm>
using namespace std;
JNIEXPORT void JNICALL Java_com_test_TestArray_callCppFun (JNIEnv *env, jobject obj)
{
jfieldID fid_array = env->GetFieldID(env->GetObjectClass(obj),"array","[I"); //获取数组的id
jintArray jint_array = (jintArray)env->GetObjectField(obj,fid_array); jint* int_arr = env->GetIntArrayElements(jint_array,NULL); //转化为jint数组
jsize len = env->GetArrayLength(jint_array);
std::sort(int_arr,int_arr+len); //对数组进行排序
env->ReleaseIntArrayElements(jint_array,int_arr,); //0:释放C++数组,并且更新到Java
/* for(jsize i=0;i<len;i++){
cout<<int_arr[i]<<endl;
}
*/
// env->ReleaseIntArrayElements(jint_array,int_arr,JNI_ABORT);//JNI_ABORT:释放C++数组,但是不更新到Java
}
上一篇:静态库打包——.a和.framework文件


下一篇:caching与缓存