zoukankan      html  css  js  c++  java
  • JNI 对象的操作

    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方法 */  
    22.     jmethodID list_add=env->GetMethodID(class_ArrayList,"add","(Ljava/lang/Object;)Z");   
    23.   
    24.     int i=0;   
    25.     while(i<3){   
    26.   
    27.         jobject student=constructStudent(env,i);   
    28.   
    29.         /* 调用List 的add方法 */  
    30.         env->CallObjectMethod(obj_List,list_add,student);   
    31.   
    32.         ++i;   
    33.     }   
    34.   
    35.        
    36.     return obj_List;   
    37.   
    38.   
    39.  }   
    40.   
    41.   
    42. /*  
    43.  * Class:     jni_StuService  
    44.  * Method:    getStudent  
    45.  * Signature: ()Ljni/Student;  
    46.  */  
    47. JNIEXPORT jobject JNICALL Java_jni_StuService_getStudent   
    48.   (JNIEnv *env, jobject obj_this)   
    49. {   
    50.     return constructStudent(env,15);   
    51. }   
    52. /*  
    53.  * 构造Student对象  
    54.  * Method:    constructStudent  
    55.  * Signature: ()Ljni/Student;  
    56.  */  
    57. jobject constructStudent(JNIEnv *env,int i ){   
    58.        
    59.     /**************创建Student 对象 start*****************/  
    60.   
    61.     jclass class_Student=env->FindClass("jni/Student");/* 获得Java类   */  
    62.   
    63.     jmethodID construct_Student=env->GetMethodID( class_Student, "<init></init>","()V");/* 获得构造方法 */  
    64.        
    65.     jobject obj_Student =env->NewObject( class_Student, construct_Student, "");/* 创建java对象 */  
    66.   
    67.     /**************创建Student 对象 end *****************/  
    68.   
    69.   
    70.     /**************创建属性ID***************************/  
    71.   
    72.     jfieldID name = env->GetFieldID(class_Student,"name","Ljava/lang/String;");   
    73.   
    74.     jfieldID age = env->GetFieldID(class_Student,"age","I");   
    75.   
    76.     /**************创建属性ID end***************************/  
    77.   
    78.   
    79.     /**************给对象的属性赋值*************************/          
    80.        
    81.     env->SetIntField(obj_Student,age,27+i);   
    82.        
    83.     env->SetObjectField(obj_Student,name,env->NewStringUTF((char*)"likun35@163.com"));   
    84.   
    85.     /**************给对象的属性赋值end *************************/  
    86.   
    87.     return obj_Student;   
    88. }  

    4. 将生成的student.dll拷贝到\WINDOWS%root%\system32下面

    5.运行StuService

  • 相关阅读:
    201301 JAVA题目0-1级, 华为笔试题
    病毒传播, 美团笔试题
    OC基础 字符串
    IOS OC语言基础(一)类的创建
    pyautogui 鼠标精灵模拟登陆
    ajax hook注入
    aiohttp异步请求
    IOS自学教程大全
    提高python工具包
    Kafka与Flume区别
  • 原文地址:https://www.cnblogs.com/yangy608/p/2114898.html
Copyright © 2011-2022 走看看