最近在项目中要监控DLL动态库的操作,在网上搜了半天,很少有关于JNI对象操作的资料,所以写了一个Demo方便大家以后搜索!
1.编写java程序,
1.1
- /**
- *
- */
- package jni;
- /**
- * @author likun
- *
- */
- public class Student {
- String name;
- int age;
- public Student(){
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String toString(){
- System.out.println("Name:"+name+" Age:"+age);
- return "Name:"+name+" Age:"+age;
- }
- }
- /**
- *
- */
- package jni;
- import java.util.Iterator;
- import java.util.List;
- /**
- * @author likun
- *
- */
- public class StuService {
- static {
- System.loadLibrary("student");
- }
- /**
- * 获得Student's List
- * @return
- */
- public static native List getStuList();
- /**
- * 返回Student对象
- * 非静态方法
- * @return
- */
- public native Student getStudent();
- public static void main(String[] args) {
- StuService stuService=new StuService();
- stuService.getStudent().toString();
- List list=StuService.getStuList();
- for(Iterator ite=list.iterator();ite.hasNext();)
- {
- Student stu=(Student)ite.next();
- stu.toString();
- }
- }
- }
声明native方法:如果你想将一个方法做为一个本地方法的话,那么你就必须声明改方法为native的,并且不能实现。
Load动态库:System.loadLibrary("student");
1.2 编译StuService.java
javac -classpath . -d . jni/StuService.java
2.生成jni_StuService.h头文件
javah -classpath . -d . jni.StuService
- /* DO NOT EDIT THIS FILE - it is machine generated */
- #include "jni.h"
- /* Header for class jni_StuService */
- #ifndef _Included_jni_StuService
- #define _Included_jni_StuService
- #ifdef __cplusplus
- extern "C" {
- #endif
- /*
- * Class: jni_StuService
- * Method: getStuList
- * Signature: ()Ljava/util/List;
- */
- JNIEXPORT jobject JNICALL Java_jni_StuService_getStuList
- (JNIEnv *, jclass);
- /*
- * Class: jni_StuService
- * Method: getStudent
- * Signature: ()Ljni/Student;
- */
- JNIEXPORT jobject JNICALL Java_jni_StuService_getStudent
- (JNIEnv *, jobject);
- /*
- * 构造Student对象
- * Method: constructStudent
- * Signature: ()Ljni/Student;
- */
- jobject constructStudent(JNIEnv *env ,int i);
- #ifdef __cplusplus
- }
- #endif
- #endif
3.在VC++环境中创建一个动态链接库的项目
3.1 File->new->Projects->Win32 Dynamic-Link Library
3.2 将jni_StuService.h加入Header Files
3.3 %root%\j2sdk1.4.2_10\include\jni.h 和%root%\j2sdk1.4.2_10\include\win32\jni_md.h加入Header Files
3.4 创建student.cpp,并实现 jni_StuService.h中的Java_jni_StuService_getStudent和Java_jni_StuService_getStuList的方法.
- #include "jni_StuService.h"
- /*
- * Class: jni_StuService
- * Method: getStuList
- * Signature: ()Ljava/util/List;
- */
- jobject JNICALL Java_jni_StuService_getStuList
- (JNIEnv *env, jclass)
- {
- /**************创建ArrayList 对象 start*****************/
- jclass class_ArrayList=env->FindClass("java/util/ArrayList");/* 获得Java类 */
- jmethodID construct=env->GetMethodID( class_ArrayList, "<init></init>","()V");/* 获得构造方法 */
- jobject obj_List =env->NewObject( class_ArrayList, construct, "");/* 创建java对象 */
- /**************创建ArrayList 对象 end *****************/
- /* 获得List的add方法 */
- jmethodID list_add=env->GetMethodID(class_ArrayList,"add","(Ljava/lang/Object;)Z");
- int i=0;
- while(i<3){
- jobject student=constructStudent(env,i);
- /* 调用List 的add方法 */
- env->CallObjectMethod(obj_List,list_add,student);
- ++i;
- }
- return obj_List;
- }
- /*
- * Class: jni_StuService
- * Method: getStudent
- * Signature: ()Ljni/Student;
- */
- JNIEXPORT jobject JNICALL Java_jni_StuService_getStudent
- (JNIEnv *env, jobject obj_this)
- {
- return constructStudent(env,15);
- }
- /*
- * 构造Student对象
- * Method: constructStudent
- * Signature: ()Ljni/Student;
- */
- jobject constructStudent(JNIEnv *env,int i ){
- /**************创建Student 对象 start*****************/
- jclass class_Student=env->FindClass("jni/Student");/* 获得Java类 */
- jmethodID construct_Student=env->GetMethodID( class_Student, "<init></init>","()V");/* 获得构造方法 */
- jobject obj_Student =env->NewObject( class_Student, construct_Student, "");/* 创建java对象 */
- /**************创建Student 对象 end *****************/
- /**************创建属性ID***************************/
- jfieldID name = env->GetFieldID(class_Student,"name","Ljava/lang/String;");
- jfieldID age = env->GetFieldID(class_Student,"age","I");
- /**************创建属性ID end***************************/
- /**************给对象的属性赋值*************************/
- env->SetIntField(obj_Student,age,27+i);
- env->SetObjectField(obj_Student,name,env->NewStringUTF((char*)"likun35@163.com"));
- /**************给对象的属性赋值end *************************/
- return obj_Student;
- }
4. 将生成的student.dll拷贝到\WINDOWS%root%\system32下面
5.运行StuService