JNI实现c++于java代码相互调用过程---李岩

    1.在D盘新建HelloWorld文件夹,文件夹中两个文件。如下

    HelloWorld.java
    ________________________________________

    import java.util.Date;
    public class HelloWorld {
        public native void print();
    public native void test(int i,String str1,String str2,int[] arr);
    public int property=888;
    public int function(int foo,Date date,int[] arr)
    {
       System.out.println("print in function()");
       return 0;
    }


        public static void main(String[] args) {
    HelloWorld hw=new HelloWorld();
    hw.print();
    int[] arr={5,6,7,8,9,0};
    hw.test(222,"string in java1","string in java2",arr);
        }
        static {
            System.loadLibrary("HelloWorld");
        }
    }
    ____________________________________________
            HelloWorld.cpp
    ______________________________________________

    #include <jni.h>
    #include <stdio.h>
    #include "HelloWorld.h"
    #include <iostream>
    using namespace std;

    JNIEXPORT void JNICALL
    Java_HelloWorld_print(JNIEnv *env, jobject obj)
    {
    //*********java调用c++中的函数*********
       cout<<"heoo"<<endl;
    cout<<"liyan "<<endl;
    //*********C++?调用java中function()并传值给java function()***********
    jclass hello_class=env->GetObjectClass( obj);//getclass
    jfieldID fieldID_prop = env ->GetFieldID(hello_class,"property","I");//get fieldid
    jmethodID methodID_function=env->GetMethodID(hello_class,"function","(ILjava/util/Date;[I)I");//get methodId
    env->CallIntMethod(obj,methodID_function,0L,NULL,NULL );//ivoke method
    jint property=env->GetIntField(obj,fieldID_prop);//get field's vlaue;
    cout<<"get proterty in java code is :"<<property<<endl;
    env->SetIntField(obj,fieldID_prop,999L);//set property in java
    jint property_beModify=env->GetIntField(obj,fieldID_prop);//get field's vlaue;
    cout<<"modify property in java by C++ is :"<<property_beModify<<endl;

    //******************************************
        return;
    }

    JNIEXPORT void JNICALL
    Java_HelloWorld_test(JNIEnv * env, jobject obj, jint i, jstring str1,jstring str2,jintArray arr)
    {
    //*********get argument form java code********************
    cout<<i<<endl;//get int value
    cout<<"test()"<<endl;
    const char* szStr1=env->GetStringUTFChars(str1,JNI_FALSE); //get string class
    cout<<szStr1<<endl;
    const char* szStr2=env->GetStringUTFChars(str2,JNI_FALSE); //get string class
    cout<<szStr2<<endl;
    env->ReleaseStringUTFChars(str1,szStr1); //release resourse
    env->ReleaseStringUTFChars(str2,szStr2); //release resourse
    //*****get array from java********************
    jint* arr_inc = env->GetIntArrayElements(arr,JNI_FALSE);
    jint number=env->GetArrayLength(arr);
    for(int j=0;j<number;j++)
    {
       cout<<arr_inc[j]<<endl;
    }
    env->ReleaseIntArrayElements(arr,arr_inc,JNI_FALSE);

    }

    2.运行cmd,进入windows命令行。进入D:\HelloWorld文件夹

            d:
            cd HelloWorld

    3.编译java

          javac HelloWorld.java

    4.为HelloWorld.cpp编译头文件

         javah -jni HelloWorld

    5.编译C++成dll文件

        cl -I%java_home%\include -I%java_home%\include\win32 -LD HelloWorld.cpp -FeHelloWorld.dll

    6.设置windows的环境变量

        把D:\HelloWorld 加入的windows的环境变量

    7.运行java class

        java HelloWorld

    8.显示出如下

    heoo
    liyan
    print in function()
    get proterty in java code is :888
    modify property in java by C++ is :999
    222
    test()
    string in java1
    string in java2
    5
    6
    7
    8
    9
    0

    如有疑问或所要代码可联系我李岩


    MSN: savagert@163.com
    Email: savagert@163.com

    李岩

    ——————————————————————————————