JNI调用的helloworld(JNI_OnLoad映射方式)

  本示例展示JNI的基本示例,helloworld级别的,不过是用JNI_OnLoad映射的方式。

  直接看代码,先看包含native method的Person.java的代码:

 1 package helloworld;
 2 
 3 /**
 4  * author : Zhou Shenshen
 5  * date   : 2016.7.16
 6  * desc   : It is a class to show how to use JNI.
 7  *          演示调用JNI本地方法的写法和加载动态库
 8  * email  : zhouss@iPanel.cn
 9  */
10 public class Person{
11 
12     /* Load the shared library libperson.so 加载C语言编写的动态库*/
13     static{
14         String classDir = Person.class.getResource("/").getPath(); 
15         System.load(classDir + "helloworld/libperson.so");
16     }
17 
18     /* Two native implemented by person.c 两个native method通过person.c来实现 */
19     public native void show();
20     public native void say(String content);
21 }

 

 这里native method的定义不用多说,注意就是调用System的load时,传入的参数是库的全名,linux下包括lib和.so,而loadLibrary方法则是传入库的名字,没有前后缀。这里采用绝对路径定位和加载库,不用在设定库的参数。

  接下来看person.c的代码:

 1 #include <jni.h>
 2 #include <stdio.h>
 3 
 4 /* Declare two function map to java native method 声明映射过来的本地函数 */
 5 void native_show(JNIEnv * env,jobject obj);
 6 void native_say(JNIEnv * env,jobject obj,jstring string);
 7 
 8 /* The array to map java method and c function 映射用的JNINativeMethod数组 */
 9 static JNINativeMethod methods[] = {
10     {"show","()V",(void *)native_show},
11     {"say","(Ljava/lang/String;)V",(void *)native_say}
12 };
13 
14 /* This function will be exec when libperson.so been loading. */
15 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved){
16     JNIEnv * env = NULL;
17     jclass cls = NULL;
18     jint result = -1;
19     if((*vm)->GetEnv(vm,(void **)&env,JNI_VERSION_1_6) != JNI_OK){
20         printf("err!!");
21         return JNI_ERR;
22     }
23     cls = (*env)->FindClass(env,"helloworld/Person");  //通过类路径字符串找到对应类
24     (*env)->RegisterNatives(env,cls,methods,sizeof(methods)/sizeof(JNINativeMethod));//调用RegisterNatives来注册本地方法,完成映射
25     return JNI_VERSION_1_6;
26 }
27 
28 /* Define the c native function. 本地方法映射过来的实现*/
29 void native_show(JNIEnv * env,jobject obj){
30     printf("hello!!\n");
31 }
32 void native_say(JNIEnv * env,jobject obj,jstring string){
33     const char * str = (*env)->GetStringUTFChars(env,string,0); //将jstring转成c语言中的字符串。
34     printf("content : %s\n",str);
35 }

  然后写个主函数测试JNI调用的情况:

 1 package helloworld;
 2 /**
 3  * author : Zhou Shenshen
 4  * date   : 2016.7.16
 5  * desc   : As the entrance for the example.
 6  * email  : zhouss@iPanel.cn
 7  */
 8 public class Main{
 9     public static void main(String[] args){
10         Person p = new Person();
11         p.show();
12         p.say("I am XiaoMing!");
13     }
14 }

  编译运行的脚本如下:

#!/bin/sh

#test the libperson.so is exist.
if [ -e libperson.so ];then
  #change to .. directory to run this example
  cd ..
  java  helloworld.Main
  exit 0;
fi

#Compiler *.java to *.class
javac Person.java Main.java
#Test the JAVA_HOME 
if [ $JAVA_HOME != ""  ]; then
  #Compiler *.c to *.o,then link to shared library *.so.
  gcc -fPIC -c -I$(echo $JAVA_HOME)/include/ -I$(echo $JAVA_HOME)/include/linux person.c
  gcc -shared -o libperson.so person.o
  #change to .. directory to run this example
  cd ..
  java helloworld.Main
  exit 0
else
  echo "JAVA_HOME is empty variable"
  exit 1
fi

  linux下bash下运行的结果如下:

hello!!
content : I am XiaoMing!

posted @ 2016-07-17 10:05  乐征Thestrals  阅读(2261)  评论(0编辑  收藏  举报