现在的位置: 首页 > 综合 > 正文

JNI 对象的操作

2012年10月20日 ⁄ 综合 ⁄ 共 3402字 ⁄ 字号 评论关闭

1.编写java程序,
1.1
 

java 代码(Student.java)

 

  1. /**  
  2.  *   
  3.  */  
  4. package jni;   
  5.   
  6. /**  
  7.  * @author likun  
  8.  *  
  9.  */  
  10. public class Student {   
  11.     String name;   
  12.     int age;   
  13.     public Student(){   
  14.            
  15.     }   
  16.     public int getAge() {   
  17.         return age;   
  18.     }   
  19.     public void setAge(int age) {   
  20.         this.age = age;   
  21.     }   
  22.     public String getName() {   
  23.         return name;   
  24.     }   
  25.     public void setName(String name) {   
  26.         this.name = name;   
  27.     }   
  28.     public String toString(){   
  29.         System.out.println("Name:"+name+"  Age:"+age);   
  30.         return "Name:"+name+"  Age:"+age;   
  31.     }   
  32. }   

 

java 代码(StuService.java)
  1. /**  
  2.  *   
  3.  */  
  4. package jni;   
  5.   
  6. import java.util.Iterator;   
  7. import java.util.List;   
  8.   
  9. /**  
  10.  * @author likun  
  11.  *  
  12.  */  
  13. public class StuService {   
  14.        
  15.     static {   
  16.         System.loadLibrary("student");   
  17.     }   
  18.        
  19.     /**  
  20.      * 获得Student's List  
  21.      * @return  
  22.      */  
  23.     public static native List getStuList();    
  24.        
  25.     /**  
  26.      * 返回Student对象  
  27.      * 非静态方法  
  28.      * @return  
  29.      */  
  30.     public  native Student getStudent();       
  31.        
  32.        
  33.     public static void main(String[] args) {   
  34.         StuService stuService=new StuService();   
  35.         stuService.getStudent().toString();   
  36.            
  37.         List list=StuService.getStuList();   
  38.         for(Iterator ite=list.iterator();ite.hasNext();)   
  39.         {   
  40.             Student stu=(Student)ite.next();   
  41.             stu.toString();   
  42.         }   
  43.            
  44.            
  45.     }   
  46.   
  47. }   

声明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)
  1. /* DO NOT EDIT THIS FILE - it is machine generated */  
  2. #include "jni.h"   
  3. /* Header for class jni_StuService */  
  4.   
  5. #ifndef _Included_jni_StuService   
  6. #define _Included_jni_StuService   
  7. #ifdef __cplusplus   
  8. extern "C" {   
  9. #endif   
  10. /*  
  11.  * Class:     jni_StuService  
  12.  * Method:    getStuList  
  13.  * Signature: ()Ljava/util/List;  
  14.  */  
  15. JNIEXPORT jobject JNICALL Java_jni_StuService_getStuList   
  16.   (JNIEnv *, jclass);   
  17.   
  18. /*  
  19.  * Class:     jni_StuService  
  20.  * Method:    getStudent  
  21.  * Signature: ()Ljni/Student;  
  22.  */  
  23. JNIEXPORT jobject JNICALL Java_jni_StuService_getStudent   
  24.   (JNIEnv *, jobject);   
  25.   
  26. /*  
  27.  * 构造Student对象  
  28.  * Method:    constructStudent  
  29.  * Signature: ()Ljni/Student;  
  30.  */  
  31. jobject constructStudent(JNIEnv *env ,int i);   
  32.   
  33. #ifdef __cplusplus   
  34. }   
  35. #endif   
  36. #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)

  1. #include "jni_StuService.h"    
  2. /*  
  3.  * Class:     jni_StuService  
  4.  * Method:    getStuList  
  5.  * Signature: ()Ljava/util/List;  
  6.  */  
  7.  jobject JNICALL Java_jni_StuService_getStuList   
  8.      (JNIEnv *env, jclass)   
  9.  {   
  10.     /**************创建ArrayList 对象 start*****************/  
  11.   
  12.     jclass class_ArrayList=env->FindClass("java/util/ArrayList");/* 获得Java类 */  
  13.   
  14.     jmethodID construct=env->GetMethodID( class_ArrayList, "<init></init>","()V");/* 获得构造方法 */  
  15.        
  16.     jobject obj_List =env->NewObject( class_ArrayList, construct, "");/* 创建java对象 */  
  17.   
  18.   
  19.     /**************创建ArrayList 对象 end *****************/  
  20.   
  21.     /* 获得List的add方法 */  

抱歉!评论已关闭.