JNI 对象的操作

本文介绍了一个使用Java Native Interface (JNI) 进行Java与C/C++混合编程的例子。通过编写Java类和对应的本地方法,再用C/C++实现这些方法,展示了如何创建并操作Java对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在项目中要监控DLL动态库的操作,在网上搜了半天,很少有关于JNI对象操作的资料,所以写了一个Demo方便大家以后搜索!

1.编写java程序,
1.1

java 代码(Student.java)

 

/**
*
*/
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;
}
}

java 代码(StuService.java)

 

/**
*
*/
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


cpp 代码(jni_StuService.h)
/* 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的方法.


cpp 代码(student.cpp)
#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值