jni的使用

来源:互联网 发布:易经入门 知乎 编辑:程序博客网 时间:2024/05/17 23:51
 class HelloWorld
{
     public native void displayHelloWorld();//所有native关键词修饰的都是对本地的声明
     public native int add(int a,int b);
     public native void printString(String str);
     public native int allAdd(int[] arr);//需要遍历该数组时,需要在加上一个参数(int[] arr, int arrayLength)
     static
     {
         System.loadLibrary("hello"); //载入本地库
     }
     public static void main(String[] args)
     {
          HelloWorld h=new HelloWorld();
          h.displayHelloWorld();
          h.printString("mynameis wurihui");
          int a=5;
          int b=6;
          System.out.println("a+b="+h.add(a,b));
          int[] arr={1,2,3,4};
          System.out.println("该数组的和为:"+h.allAdd(arr));    
         
     }
}

1. 使用命令javac HelloWorld.java编译成.class文件
2.  在.class文件编译成.h文件的过程中:
需要先:set classpath=C:\Users\rihui\Desktop\jni(你的.class文件所在位置)
然后:javah -jni HelloWorld

2.或者直接javah HelloWorld

生成的HelloWorld.h文件
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */

#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:     HelloWorld
* Method:    displayHelloWorld
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
  (JNIEnv *, jobject);

/*
* Class:     HelloWorld
* Method:    add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_HelloWorld_add
  (JNIEnv *, jobject, jint, jint);

/*
* Class:     HelloWorld
* Method:    printString
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_HelloWorld_printString
  (JNIEnv *, jobject, jstring);

/*
* Class:     HelloWorld
* Method:    allAdd
* Signature: ([I)I
*/
JNIEXPORT jint JNICALL Java_HelloWorld_allAdd
  (JNIEnv *, jobject, jintArray);

#ifdef __cplusplus
}
#endif
#endif

3.生成hello.dll(在windows底下是dll文件,linux系统下是so文件) 与Java文件同名System.loadLibrary("hello"); 
 
使用visual c++6.0新建一个工程
取名为hello ,这样build后就会生成一个hello.dll文件,当然你也可以随意取名,
最后在改为hello.dll就好了,在vs中实现HelloWorld.h中方法,也就是java中的native方法
#include "HelloWorld.h"
#include <iostream>
using namespace std;

JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld (JNIEnv *, jobject)
{
     cout<<"hello rihui1hao"<<endl;
}

JNIEXPORT jint JNICALL Java_HelloWorld_add (JNIEnv *, jobject, jint a, jint b)
{
     return a+b;
}

JNIEXPORT void JNICALL Java_HelloWorld_printString (JNIEnv *env, jobject obj, jstring s)
{
     const char* str;
    str = env->GetStringUTFChars(s, false);
     if (str!=NULL)
     {
          cout<<str<<endl;
     }
     else
     {
          cout<<"null"<<endl;
     }
     env->ReleaseStringUTFChars(s, str);
}

JNIEXPORT jint JNICALL Java_HelloWorld_allAdd (JNIEnv *env, jobject obj, jintArray arr)
{
     jint *carr;
    
    carr = env->GetIntArrayElements(arr, false);
    
    if(carr == NULL) {
         
        return 0; /* exception occurred */
         
    }
    
    jint sum = 0;
     cout<<"carr[0]="<<carr[0]<<endl;
     cout<<"carr[4]="<<carr[4]<<endl;
    for(int i=0; i<4; i++) {
         
        sum += carr[i];
         
    }
    
    env->ReleaseIntArrayElements(arr, carr, 0);
    
    return sum;
}