-
-
-
- package jni;
-
- import java.util.Iterator;
- import java.util.List;
-
-
-
-
-
- public class StuService {
-
- static {
- System.loadLibrary("student");
- }
-
-
-
-
-
- public static native List getStuList();
-
-
-
-
-
-
- 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)
-
- #include "jni.h"
-
-
- #ifndef _Included_jni_StuService
- #define _Included_jni_StuService
- #ifdef __cplusplus
- extern "C" {
- #endif
-
-
-
-
-
- JNIEXPORT jobject JNICALL Java_jni_StuService_getStuList
- (JNIEnv *, jclass);
-
-
-
-
-
-
- JNIEXPORT jobject JNICALL Java_jni_StuService_getStudent
- (JNIEnv *, jobject);
-
-
-
-
-
-
- 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"
-
-
-
-
-
- jobject JNICALL Java_jni_StuService_getStuList
- (JNIEnv *env, jclass)
- {
-
-
- jclass class_ArrayList=env->FindClass("java/util/ArrayList");
-
- jmethodID construct=env->GetMethodID( class_ArrayList, "<init></init>","()V");
-
- jobject obj_List =env->NewObject( class_ArrayList, construct, "");
-
-
-
-
-
- jmethodID list_add=env->GetMethodID(class_ArrayList,"add","(Ljava/lang/Object;)Z");
-
- int i=0;
- while(i<3){
-
- jobject student=constructStudent(env,i);
-
-
- env->CallObjectMethod(obj_List,list_add,student);
-
- ++i;
- }
-
-
- return obj_List;
-
-
- }
-
-
-
-
-
-
-
- JNIEXPORT jobject JNICALL Java_jni_StuService_getStudent
- (JNIEnv *env, jobject obj_this)
- {
- return constructStudent(env,15);
- }
-
-
-
-
-
- jobject constructStudent(JNIEnv *env,int i ){
-
-
-
- jclass class_Student=env->FindClass("jni/Student");
-
- jmethodID construct_Student=env->GetMethodID( class_Student, "<init></init>","()V");
-
- jobject obj_Student =env->NewObject( class_Student, construct_Student, "");
-
-
-
-
-
-
- jfieldID name = env->GetFieldID(class_Student,"name","Ljava/lang/String;");
-
- jfieldID age = env->GetFieldID(class_Student,"age","I");
-
-
-
-
-
-
- env->SetIntField(obj_Student,age,27+i);
-
- env->SetObjectField(obj_Student,name,env->NewStringUTF((char*)"likun35@163.com"));
-
-
-
- return obj_Student;
- }
4. 将生成的student.dll拷贝到\WINDOWS%root%\system32下面
5.运行StuService