御风 2011-10-28
JavaJNI由浅入深(包括:Java和C++互传ArrayList泛型对象参数)
2010-11-2509:571168人阅读评论(1)收藏举报
我们知道Java是一个运行在虚拟机里面的高级的编程语言,如果要调用系统的动态链接库的话,就要先声明native修饰的方法(类似接口里面的方法),再由C/C++程序来实现(类似实现接口里的方法)。这样Java调用这些native方法就相当于调用了C/C++里面实现了的方法。通常我们把这种机制叫做JNI(JavaNativeInterfac),即Java本地编程接口
Android也同理,要学会在Android上进行NDK开发,首先我们到打好javaJNI的基础。现在我们暂时把Android开发丢到一边先,试试在Java之下编译一个C动态链接库,再用Java程序调用。
1)先来个最简单的打印HelloWorld例子:
Java代码(HelloJni.java):
viewplainprint?
1.importjava.util.*;
2.publicclassHelloJni{
3.
4.static{
5.System.loadLibrary("hello");
6.}
7.publicnativestaticvoidsayHello();
8.publicstaticvoidmain(String[]args)
9.{
10.HelloJni.sayHello();
11.}
12.}
importjava.util.*;publicclassHelloJni{static{System.loadLibrary("hello");}publicnativestaticvoidsayHello();publicstaticvoidmain(String[]args){HelloJni.sayHello();}}
生成头文件(HelloJni.h):
先javacHelloJni.java编译你的Java源码,再javah–jniHelloJni生成所需的头文件
头文件内容是这样的:
viewplainprint?
1./*DONOTEDITTHISFILE-itismachinegenerated*/
2.#include<jni.h>
3./*HeaderforclassHelloJni*/
4.#ifndef_Included_HelloJni
5.#define_Included_HelloJni
6.#ifdef__cplusplus
7.extern"C"{
8.#endif
9.JNIEXPORTvoidJNICALLJava_HelloJni_sayHello(JNIEnv*,jclass);
10.#ifdef__cplusplus
11.}
12.#endif
13.#endif
/*DONOTEDITTHISFILE-itismachinegenerated*/#include<jni.h>/*HeaderforclassHelloJni*/#ifndef_Included_HelloJni#define_Included_HelloJni#ifdef__cplusplusextern"C"{#endifJNIEXPORTvoidJNICALLJava_HelloJni_sayHello(JNIEnv*,jclass);#ifdef__cplusplus}#endif#endif
实现头文件声明的方法(HelloJni.cpp)
viewplainprint?
1.#include"HelloJni.h"
2.JNIEXPORTvoidJNICALLJava_HelloJni_sayHello(JNIEnv*env,jclasscls)
3.{
4.printf("HelloWorld");
5.}
#include"HelloJni.h"JNIEXPORTvoidJNICALLJava_HelloJni_sayHello(JNIEnv*env,jclasscls){printf("HelloWorld");}
编译命令:g++-I$JAVA_HOME/include-I$JAVA_HOME/include/linuxHelloJni.cpp-shared-olibhello.so
命令参数解析:-I是指引入java虚拟机的库的路径,-shared是指编译成动态链接库(共享库)–o输出文件名(注意,在Linux平台下的动态链接库有一个命名格式:“lib+库名+.so”在java代码里面loadLibrary的时候不要加lib前缀和.so后缀)
由于我这里把这个动态链接库编译放在当前目录下,所以还要设置环境变量LD_LIBRARY_PATH=该so动态链接库所在的目录,才能正常运行
#############################+++华丽的分割线+++###########################
2)好了,可以打印HelloWorld出来后,我们再深入一点点,传入一个int的数,在C/C++代码里面加1后返回。
Java代码(HelloJni.java):
viewplainprint?
1.publicclassHelloJni{
2.static{
3.System.loadLibrary("hello");
4.}
5.publicstaticnativevoidsayHello();
6.publicnativeintgetInt();
7.publicnativevoidsetInt(inti);
8.publicstaticvoidmain(Stringargs[]){
9.//HelloJni.sayHello();
10.HelloJnihello=newHelloJni();
11.hello.setInt(2);
12.System.out.println(hello.getInt());
13.}
14.}
publicclassHelloJni{static{System.loadLibrary("hello");}publicstaticnativevoidsayHello();publicnativeintgetInt();publicnativevoidsetInt(inti);publicstaticvoidmain(Stringargs[]){//HelloJni.sayHello();HelloJnihello=newHelloJni();hello.setInt(2);System.out.println(hello.getInt());}}
生成头文件(HelloJni.h):
viewplainprint?
1./*DONOTEDITTHISFILE-itismachinegenerated*/
2.#include<jni.h>
3./*HeaderforclassHelloJni*/
4.#ifndef_Included_HelloJni
5.#define_Included_HelloJni
6.#ifdef__cplusplus
7.extern"C"{
8.#endif
9.JNIEXPORTvoidJNICALLJava_HelloJni_sayHello(JNIEnv*,jclass);
10.JNIEXPORTjintJNICALLJava_HelloJni_getInt(JNIEnv*,jobject);
11.JNIEXPORTvoidJNICALLJava_HelloJni_setInt(JNIEnv*,jobject,jint);
12.#ifdef__cplusplus
13.}
14.#endif
15.#endif
/*DONOTEDITTHISFILE-itismachinegenerated*/#include<jni.h>/*HeaderforclassHelloJni*/#ifndef_Included_HelloJni#define_Included_HelloJni#ifdef__cplusplusextern"C"{#endifJNIEXPORTvoidJNICALLJava_HelloJni_sayHello(JNIEnv*,jclass);JNIEXPORTjintJNICALLJava_HelloJni_getInt(JNIEnv*,jobject);JNIEXPORTvoidJNICALLJava_HelloJni_setInt(JNIEnv*,jobject,jint);#ifdef__cplusplus}#endif#endif
实现头文件声明的方法(HelloJni.cpp)
viewplainprint?
1.#include"HelloJni.h"
2.inti=1;
3.JNIEXPORTvoidJNICALLJava_HelloJni_sayHello
4.(JNIEnv*env,jclasscls){
5.printf("HelloWorld/n");
6.}
7.JNIEXPORTjintJNICALLJava_HelloJni_getInt
8.(JNIEnv*env,jobjectthiz){
9.returni;
10.}
11.JNIEXPORTvoidJNICALLJava_HelloJni_setInt
12.(JNIEnv*env,jobjectthiz,jintji){
13.i=ji+1;
14.}
#include"HelloJni.h"inti=1;JNIEXPORTvoidJNICALLJava_HelloJni_sayHello(JNIEnv*env,jclasscls){printf("HelloWorld/n");}JNIEXPORTjintJNICALLJava_HelloJni_getInt(JNIEnv*env,jobjectthiz){returni;}JNIEXPORTvoidJNICALLJava_HelloJni_setInt(JNIEnv*env,jobjectthiz,jintji){i=ji+1;}
运行结果:打印2
3)只是传入简单的数据类型不爽,这次让C/C++生成个Java对象返回
Java代码:pojo实体类(User.java)
viewplainprint?
1.publicclassUser{
2.privatelongid;
3.privateStringuserName;
4.privatebooleanisMan;
5.privateintage;
6.publicUser(){}
7.publicUser(longid,StringuserName,booleanisMan,intage){
8.super();
9.this.id=id;
10.this.userName=userName;
11.this.isMan=isMan;
12.this.age=age;
13.}
14.publiclonggetId(){
15.returnid;
16.}
17.publicvoidsetId(longid){
18.this.id=id;
19.}
20.publicStringgetUserName(){
21.returnuserName;
22.}
23.publicvoidsetUserName(StringuserName){
24.this.userName=userName;
25.}
26.publicbooleanisMan(){
27.returnisMan;
28.}
29.publicvoidsetMan(booleanisMan){
30.this.isMan=isMan;
31.}
32.publicintgetAge(){
33.returnage;
34.}
35.publicvoidsetAge(intage){
36.this.age=age;
37.}
38.}
publicclassUser{privatelongid;privateStringuserName;privatebooleanisMan;privateintage;publicUser(){}publicUser(longid,StringuserName,booleanisMan,intage){super();this.id=id;this.userName=userName;this.isMan=isMan;this.age=age;}publiclonggetId(){returnid;}publicvoidsetId(longid){this.id=id;}publicStringgetUserName(){returnuserName;}publicvoidsetUserName(StringuserName){this.userName=userName;}publicbooleanisMan(){returnisMan;}publicvoidsetMan(booleanisMan){this.isMan=isMan;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}}
Java代码(HelloJni.java):
viewplainprint?
1.publicclassHelloJni{
2.static{
3.System.loadLibrary("userbean");
4.}
5.publicstaticnativevoidsayHello();
6.publicnativeintgetInt();
7.publicnativevoidsetInt(inti);
8.publicnativevoidsetUser(StringuserName);
9.publicnativeUsergetUser();
10.publicstaticvoidmain(Stringargs[]){
11.//HelloJni.sayHello();
12.HelloJnihello=newHelloJni();
13.//hello.setInt(2);
14.//System.out.println(hello.getInt());
15.hello.setUser("LiangYaotian");
16.Useruser=hello.getUser();
17.System.out.println("userfromc/c++");
18.System.out.println("name:"+user.getUserName());
19.System.out.println("isMan?:"+user.isMan());
20.System.out.println("age:"+user.getAge());
21.}
22.}
publicclassHelloJni{static{System.loadLibrary("userbean");}publicstaticnativevoidsayHello();publicnativeintgetInt();publicnativevoidsetInt(inti);publicnativevoidsetUser(StringuserName);publicnativeUsergetUser();publicstaticvoidmain(Stringargs[]){//HelloJni.sayHello();HelloJnihello=newHelloJni();//hello.setInt(2);//System.out.println(hello.getInt());hello.setUser("LiangYaotian");Useruser=hello.getUser();System.out.println("userfromc/c++");System.out.println("name:"+user.getUserName());System.out.println("isMan?:"+user.isMan());System.out.println("age:"+user.getAge());}}
生成头文件(HelloJni.h):
viewplainprint?
1.#include<jni.h>
2./*HeaderforclassHelloJni*/
3.#ifndef_Included_HelloJni
4.#define_Included_HelloJni
5.#ifdef__cplusplus
6.extern"C"{
7.#endif
8.JNIEXPORTvoidJNICALLJava_HelloJni_sayHello(JNIEnv*,jclass);
9.JNIEXPORTjintJNICALLJava_HelloJni_getInt(JNIEnv*,jobject);
10.JNIEXPORTvoidJNICALLJava_HelloJni_setInt(JNIEnv*,jobject,jint);
11.JNIEXPORTvoidJNICALLJava_HelloJni_setUser(JNIEnv*,jobject,jstring);
12.JNIEXPORTjobjectJNICALLJava_HelloJni_getUser(JNIEnv*,jobject);
13.#ifdef__cplusplus
14.}
15.#endif
16.#endif
#include<jni.h>/*HeaderforclassHelloJni*/#ifndef_Included_HelloJni#define_Included_HelloJni#ifdef__cplusplusextern"C"{#endifJNIEXPORTvoidJNICALLJava_HelloJni_sayHello(JNIEnv*,jclass);JNIEXPORTjintJNICALLJava_HelloJni_getInt(JNIEnv*,jobject);JNIEXPORTvoidJNICALLJava_HelloJni_setInt(JNIEnv*,jobject,jint);JNIEXPORTvoidJNICALLJava_HelloJni_setUser(JNIEnv*,jobject,jstring);JNIEXPORTjobjectJNICALLJava_HelloJni_getUser(JNIEnv*,jobject);#ifdef__cplusplus}#endif#endif
实现头文件声明的方法(HelloJni.cpp)
viewplainprint?
1.#include<iostream>
2.#include<stdio.h>
3.usingnamespacestd;
4.inti=1;
5.jobjectuser;
6.JNIEXPORTvoidJNICALLJava_HelloJni_sayHello
7.(JNIEnv*env,jclasscls){
8.printf("HelloWorld/n");
9.}
10.JNIEXPORTjintJNICALLJava_HelloJni_getInt
11.(JNIEnv*env,jobjectthiz){
12.returni;
13.}
14.JNIEXPORTvoidJNICALLJava_HelloJni_setInt
15.(JNIEnv*env,jobjectthiz,jintji){
16.i=ji+1;
17.}
18.JNIEXPORTvoidJNICALLJava_HelloJni_setUser
19.(JNIEnv*env,jobjectthiz,jstringname){
20.jclassuserClass=env->FindClass("User");
21.jmethodIDuserMethod=env->GetMethodID(userClass,"<init>","()V");
22.jfieldIDmId=env->GetFieldID(userClass,"id","J");
23.jfieldIDmUserName=env->GetFieldID(userClass,"userName","Ljava/lang/String;");
24.jfieldIDmIsMan=env->GetFieldID(userClass,"isMan","Z");
25.jfieldIDmAge=env->GetFieldID(userClass,"age","I");
26.jobjectuserObject=env->NewObject(userClass,userMethod);
27.env->SetObjectField(userObject,mUserName,name);
28.env->SetLongField(userObject,mId,1001);
29.env->SetBooleanField(userObject,mIsMan,1);
30.env->SetIntField(userObject,mAge,21);
31.user=userObject;
32.}
#include<iostream>#include<stdio.h>usingnamespacestd;inti=1;jobjectuser;JNIEXPORTvoidJNICALLJava_HelloJni_sayHello(JNIEnv*env,jclasscls){printf("HelloWorld/n");}JNIEXPORTjintJNICALLJava_HelloJni_getInt(JNIEnv*env,jobjectthiz){returni;}JNIEXPORTvoidJNICALLJava_HelloJni_setInt(JNIEnv*env,jobjectthiz,jintji){i=ji+1;}JNIEXPORTvoidJNICALLJava_HelloJni_setUser(JNIEnv*env,jobjectthiz,jstringname){jclassuserClass=env->FindClass("User");jmethodIDuserMethod=env->GetMethodID(userClass,"<init>","()V");jfieldIDmId=env->GetFieldID(userClass,"id","J");jfieldIDmUserName=env->GetFieldID(userClass,"userName","Ljava/lang/String;");jfieldIDmIsMan=env->GetFieldID(userClass,"isMan","Z");jfieldIDmAge=env->GetFieldID(userClass,"age","I");jobjectuserObject=env->NewObject(userClass,userMethod);env->SetObjectField(userObject,mUserName,name);env->SetLongField(userObject,mId,1001);env->SetBooleanField(userObject,mIsMan,1);env->SetIntField(userObject,mAge,21);user=userObject;}
4)有些同学可能会说:“返回个Java对象算什么啊,C/C++和Java之间互传复杂对象才好玩呢!”正所谓“不怕做不到,就怕想不到”,我们接着重构一下上面那个例子!
这次我们传包含有User对象的List到C/C++程序里面
Java代码(User.java)
同上。
Java代码(HelloJni.java)
viewplainprint?
1.importjava.util.*;
2.publicclassHelloJni{
3.static{
4.System.loadLibrary("userbean");
5.}
6.publicnativeintget();
7.publicnativevoidset(inti);
8.publicnativevoidsetUser(StringuserName);
9.publicnativeUsergetUser();
10.publicnativevoidsetUserList(ArrayList<User>userList);
11.publicnativeArrayList<User>getUserList();
12.publicstaticvoidmain(String[]args)
13.{
14.HelloJnihello=newHelloJni();
15.//hello.set(2);
16.//System.out.println(hello.get());
17./*
18.hello.setUser("LiangYaotian");
19.Useruser=hello.getUser();
20.System.out.println("userfromc/c++");
21.System.out.println("name:"+user.getUserName());
22.System.out.println("isMan?:"+user.isMan());
23.System.out.println("age:"+user.getAge());
24.*/
25.ArrayList<User>userList=newArrayList<User>();
26.for(inti=0;i<10;i++){
27.Useru=newUser((long)(1000+i),"LiangYaotian"+i,true,21);
28.userList.add(u);
29.}
30.hello.setUserList(userList);
31.userList=null;
32.userList=hello.getUserList();
33.System.out.println("ArrayList<User>constructfromC++,thenJavaprintit.....");
34.for(Useru:userList){
35.System.out.println("id:"+u.getId()+";name:"+u.getUserName());
36.}
37.}
38.}
importjava.util.*;publicclassHelloJni{static{System.loadLibrary("userbean");}publicnativeintget();publicnativevoidset(inti);publicnativevoidsetUser(StringuserName);publicnativeUsergetUser();publicnativevoidsetUserList(ArrayList<User>userList);publicnativeArrayList<User>getUserList();publicstaticvoidmain(String[]args){HelloJnihello=newHelloJni();//hello.set(2);//System.out.println(hello.get());/*hello.setUser("LiangYaotian");Useruser=hello.getUser();System.out.println("userfromc/c++");System.out.println("name:"+user.getUserName());System.out.println("isMan?:"+user.isMan());System.out.println("age:"+user.getAge());*/ArrayList<User>userList=newArrayList<User>();for(inti=0;i<10;i++){Useru=newUser((long)(1000+i),"LiangYaotian"+i,true,21);userList.add(u);}hello.setUserList(userList);userList=null;userList=hello.getUserList();System.out.println("ArrayList<User>constructfromC++,thenJavaprintit.....");for(Useru:userList){System.out.println("id:"+u.getId()+";name:"+u.getUserName());}}}
C头文件(HelloJni.h)
viewplainprint?
1.#include<jni.h>
2./*HeaderforclassHelloJni*/
3.#ifndef_Included_HelloJni
4.#define_Included_HelloJni
5.#ifdef__cplusplus
6.extern"C"{
7.#endif
8.JNIEXPORTvoidJNICALLJava_HelloJni_sayHello(JNIEnv*,jclass);
9.JNIEXPORTjintJNICALLJava_HelloJni_getInt(JNIEnv*,jobject);
10.JNIEXPORTvoidJNICALLJava_HelloJni_setInt(JNIEnv*,jobject,jint);
11.JNIEXPORTvoidJNICALLJava_HelloJni_setUser(JNIEnv*,jobject,jstring);
12.JNIEXPORTjobjectJNICALLJava_HelloJni_getUser(JNIEnv*,jobject);
#include<jni.h>/*HeaderforclassHelloJni*/#ifndef_Included_HelloJni#define_Included_HelloJni#ifdef__cplusplusextern"C"{#endifJNIEXPORTvoidJNICALLJava_HelloJni_sayHello(JNIEnv*,jclass);JNIEXPORTjintJNICALLJava_HelloJni_getInt(JNIEnv*,jobject);JNIEXPORTvoidJNICALLJava_HelloJni_setInt(JNIEnv*,jobject,jint);JNIEXPORTvoidJNICALLJava_HelloJni_setUser(JNIEnv*,jobject,jstring);JNIEXPORTjobjectJNICALLJava_HelloJni_getUser(JNIEnv*,jobject);
头文件的实现(HelloJni.cpp)
viewplainprint?
1.#include"HelloJni.h"
2.inti=1;
3.jobjectuser;
4.JNIEXPORTjintJNICALLJava_HelloJni_get
5.(JNIEnv*env,jobjectjthiz){
6.printf("HelloWorld/n");
7.}
8.JNIEXPORTvoidJNICALLJava_HelloJni_set
9.(JNIEnv*env,jobjectjthiz,jintji){
10.i=ji+1;
11.}
12.JNIEXPORTvoidJNICALLJava_HelloJni_setUser
13.(JNIEnv*env,jobjectjthiz,jstringname){
14.jclassuserClass=env->FindClass("User");
15.jmethodIDuserMethod=env->GetMethodID(userClass,"<init>","()V");
16.jfieldIDmId=env->GetFieldID(userClass,"id","J");
17.jfieldIDmUserName=env->GetFieldID(userClass,"userName","Ljava/lang/String;");
18.jfieldIDmIsMan=env->GetFieldID(userClass,"isMan","Z");
19.jfieldIDmAge=env->GetFieldID(userClass,"age","I");
20.jobjectuserObject=env->NewObject(userClass,userMethod);
21.env->SetObjectField(userObject,mUserName,name);
22.env->SetLongField(userObject,mId,1001);
23.env->SetBooleanField(userObject,mIsMan,1);
24.env->SetIntField(userObject,mAge,21);
25.user=userObject;
26.}
27.JNIEXPORTjobjectJNICALLJava_HelloJni_getUser
28.(JNIEnv*env,jobjectjthiz){
29.returnuser;
30.}
31.JNIEXPORTvoidJNICALLJava_HelloJni_setUserList
32.(JNIEnv*env,jobjectjthiz,jobjectuserList){
33.inti;
34.//classArrayList
35.jclasscls_arraylist=env->GetObjectClass(userList);
36.//methodinclassArrayList
37.jmethodIDarraylist_get=env->GetMethodID(cls_arraylist,"get","(I)Ljava/lang/Object;");
38.jmethodIDarraylist_size=env->GetMethodID(cls_arraylist,"size","()I");
39.jintlen=env->CallIntMethod(userList,arraylist_size);
40.printf("getjavaArrayList<User>objectbyC++,thenprintit...../n");
41.for(i=0;i<len;i++){
42.jobjectobj_user=env->CallObjectMethod(userList,arraylist_get,i);
43.jclasscls_user=env->GetObjectClass(obj_user);
44.jmethodIDuser_getId=env->GetMethodID(cls_user,"getId","()J");
45.jmethodIDuser_getUserName=env->GetMethodID(cls_user,"getUserName","()Ljava/lang/String;");
46.jmethodIDuser_isMan=env->GetMethodID(cls_user,"isMan","()Z");
47.jmethodIDuser_getAge=env->GetMethodID(cls_user,"getAge","()I");
48.jstringname=(jstring)env->CallObjectMethod(obj_user,user_getUserName);
49.jbooleanb=true;
50.constchar*namePtr=env->GetStringUTFChars(name,&b);
51.jlongid=env->CallLongMethod(obj_user,user_getId);
52.jbooleansex=env->CallBooleanMethod(obj_user,user_isMan);
53.jintage=env->CallIntMethod(obj_user,user_getAge);
54.printf("Id:%d;",id);
55.printf("Name:%s;",namePtr);
56.printf("isMan?%d;",sex);
57.printf("Age:%d/n",age);
58.}
59.}
60.JNIEXPORTjobjectJNICALLJava_HelloJni_getUserList
61.(JNIEnv*env,jobjectjthiz){
62.//ArrayListObject
63.jclasscls_ArrayList=env->FindClass("java/util/ArrayList");
64.jmethodIDconstruct=env->GetMethodID(cls_ArrayList,"<init>","()V");
65.jobjectobj_ArrayList=env->NewObject(cls_ArrayList,construct,"");
66.jmethodIDarrayList_add=env->GetMethodID(cls_ArrayList,"add","(Ljava/lang/Object;)Z");
67.//UserObject
68.jclasscls_user=env->FindClass("User");
69.//noneargumentconstructfunction
70.jmethodIDconstruct_user=env->GetMethodID(cls_user,"<init>","()V");
71.//newaobject
72.jobjectobj_user=env->NewObject(cls_user,construct_user,"");
73.//getmethodid
74./*
75.jmethodIDuser_setId=env->GetMethodID(cls_user,"setId","(J)V");
76.jmethodIDuser_setUserName=env->GetMethodID(cls_user,"setUserName","(Ljava/lang/String;)V");
77.jmethodIDuser_setMan=env->GetMethodID(cls_user,"setMan","(Z)V");
78.jmethodIDuser_setAge=env->GetMethodID(cls_user,"setAge","(I)V");
79.*/
80.inti;
81.for(i=0;i<10;i++){
82.//newaobject
83.jobjectobj_user=env->NewObject(cls_user,construct_user,"");
84.//getfieldid
85.jfieldIDuser_id=env->GetFieldID(cls_user,"id","J");
86.jfieldIDuser_name=env->GetFieldID(cls_user,"userName","Ljava/lang/String;");
87.jfieldIDuser_isMan=env->GetFieldID(cls_user,"isMan","Z");
88.jfieldIDuser_age=env->GetFieldID(cls_user,"age","I");
89.env->SetLongField(obj_user,user_id,i);
90.env->SetObjectField(obj_user,user_name,env->NewStringUTF("LiangYaoTian"));
91.env->SetBooleanField(obj_user,user_isMan,1);
92.env->SetIntField(obj_user,user_age,21);
93.env->CallObjectMethod(obj_ArrayList,arrayList_add,obj_user);
94.}
95.returnobj_ArrayList;
96.}
#include"HelloJni.h"inti=1;jobjectuser;JNIEXPORTjintJNICALLJava_HelloJni_get(JNIEnv*env,jobjectjthiz){printf("HelloWorld/n");}JNIEXPORTvoidJNICALLJava_HelloJni_set(JNIEnv*env,jobjectjthiz,jintji){i=ji+1;}JNIEXPORTvoidJNICALLJava_HelloJni_setUser(JNIEnv*env,jobjectjthiz,jstringname){jclassuserClass=env->FindClass("User");jmethodIDuserMethod=env->GetMethodID(userClass,"<init>","()V");jfieldIDmId=env->GetFieldID(userClass,"id","J");jfieldIDmUserName=env->GetFieldID(userClass,"userName","Ljava/lang/String;");jfieldIDmIsMan=env->GetFieldID(userClass,"isMan","Z");jfieldIDmAge=env->GetFieldID(userClass,"age","I");jobjectuserObject=env->NewObject(userClass,userMethod);env->SetObjectField(userObject,mUserName,name);env->SetLongField(userObject,mId,1001);env->SetBooleanField(userObject,mIsMan,1);env->SetIntField(userObject,mAge,21);user=userObject;}JNIEXPORTjobjectJNICALLJava_HelloJni_getUser(JNIEnv*env,jobjectjthiz){returnuser;}JNIEXPORTvoidJNICALLJava_HelloJni_setUserList(JNIEnv*env,jobjectjthiz,jobjectuserList){inti;//classArrayListjclasscls_arraylist=env->GetObjectClass(userList);//methodinclassArrayListjmethodIDarraylist_get=env->GetMethodID(cls_arraylist,"get","(I)Ljava/lang/Object;");jmethodIDarraylist_size=env->GetMethodID(cls_arraylist,"size","()I");jintlen=env->CallIntMethod(userList,arraylist_size);printf("getjavaArrayList<User>objectbyC++,thenprintit...../n");for(i=0;i<len;i++){jobjectobj_user=env->CallObjectMethod(userList,arraylist_get,i);jclasscls_user=env->GetObjectClass(obj_user);jmethodIDuser_getId=env->GetMethodID(cls_user,"getId","()J");jmethodIDuser_getUserName=env->GetMethodID(cls_user,"getUserName","()Ljava/lang/String;");jmethodIDuser_isMan=env->GetMethodID(cls_user,"isMan","()Z");jmethodIDuser_getAge=env->GetMethodID(cls_user,"getAge","()I");jstringname=(jstring)env->CallObjectMethod(obj_user,user_getUserName);jbooleanb=true;constchar*namePtr=env->GetStringUTFChars(name,&b);jlongid=env->CallLongMethod(obj_user,user_getId);jbooleansex=env->CallBooleanMethod(obj_user,user_isMan);jintage=env->CallIntMethod(obj_user,user_getAge);printf("Id:%d;",id);printf("Name:%s;",namePtr);printf("isMan?%d;",sex);printf("Age:%d/n",age);}}JNIEXPORTjobjectJNICALLJava_HelloJni_getUserList(JNIEnv*env,jobjectjthiz){//ArrayListObjectjclasscls_ArrayList=env->FindClass("java/util/ArrayList");jmethodIDconstruct=env->GetMethodID(cls_ArrayList,"<init>","()V");jobjectobj_ArrayList=env->NewObject(cls_ArrayList,construct,"");jmethodIDarrayList_add=env->GetMethodID(cls_ArrayList,"add","(Ljava/lang/Object;)Z");//UserObjectjclasscls_user=env->FindClass("User");//noneargumentconstructfunctionjmethodIDconstruct_user=env->GetMethodID(cls_user,"<init>","()V");//newaobjectjobjectobj_user=env->NewObject(cls_user,construct_user,"");//getmethodid/*jmethodIDuser_setId=env->GetMethodID(cls_user,"setId","(J)V");jmethodIDuser_setUserName=env->GetMethodID(cls_user,"setUserName","(Ljava/lang/String;)V");jmethodIDuser_setMan=env->GetMethodID(cls_user,"setMan","(Z)V");jmethodIDuser_setAge=env->GetMethodID(cls_user,"setAge","(I)V");*/inti;for(i=0;i<10;i++){//newaobjectjobjectobj_user=env->NewObject(cls_user,construct_user,"");//getfieldidjfieldIDuser_id=env->GetFieldID(cls_user,"id","J");jfieldIDuser_name=env->GetFieldID(cls_user,"userName","Ljava/lang/String;");jfieldIDuser_isMan=env->GetFieldID(cls_user,"isMan","Z");jfieldIDuser_age=env->GetFieldID(cls_user,"age","I");env->SetLongField(obj_user,user_id,i);env->SetObjectField(obj_user,user_name,env->NewStringUTF("LiangYaoTian"));env->SetBooleanField(obj_user,user_isMan,1);env->SetIntField(obj_user,user_age,21);env->CallObjectMethod(obj_ArrayList,arrayList_add,obj_user);}returnobj_ArrayList;}
运行结果:
theEnd.jpg
源码:E-Mail我:[email protected]
原创作品,转载需注明出处...http://blog.csdn.net/u_xtian/archive/2010/11/25/6033963.aspx