3 linux下jni的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_16949707/article/details/61195348

一 配置linux环境并测试

1 java环境
http://www.jianshu.com/p/e5d8b62aed89
2 linux怎么跑跑java程序
2.1 错误代码
文件名与类名要相同

public class HelloWorld {
    public static void main(String args[]) {
        System.out.println("HelloWorld");  
     }
}

2.2 正确代码

public class hello_java{
    public static void main(String args[]) {
        System.out.println("HelloWorld");  
     }
}

2.3 效果

root@52acd6c79e97:/home/huxiang_j/work/jni/java/test_java_hello_world_3_10# vim.tiny hello_java.java
root@52acd6c79e97:/home/huxiang_j/work/jni/java/test_java_hello_world_3_10# javac hello_java.java 
hello_java.java:1: error: class HelloWorld is public, should be declared in a file named HelloWorld.java
public class HelloWorld {
       ^
1 error
root@52acd6c79e97:/home/huxiang_j/work/jni/java/test_java_hello_world_3_10# vim.tiny hello_java.java 
root@52acd6c79e97:/home/huxiang_j/work/jni/java/test_java_hello_world_3_10# !javac
javac hello_java.java 
root@52acd6c79e97:/home/huxiang_j/work/jni/java/test_java_hello_world_3_10# ls
hello_java.class  hello_java.java
root@52acd6c79e97:/home/huxiang_j/work/jni/java/test_java_hello_world_3_10# java hello_java 
HelloWorld

二 linux下使用jni来实现java对c++的调用

1 创建文件目录

[hx133330@e69d19429.et15sqa /home/hx133330/work/jni]
$cd TestNativeCode/src/com/wwj/nativecode/

[hx133330@e69d19429.et15sqa /home/hx133330/work/jni/TestNativeCode/src/com/wwj/nativecode]
$ls
TestNativeCode.java

2 编写TestNativeCode.java文件

//3 send abc return cba  dll path 
package com.jni.demo; 
public class JNIDemo {
   //定义一个本地方法  
   public native void sayHello();  
   public native String changeString(String src);
//   public native void setInt(String src);
   public static void main(String[] args){  
       //调用动态链接库  
       System.loadLibrary("nativeCode");  
       JNIDemo jniDemo = new JNIDemo();  
       jniDemo.sayHello();  
       System.out.println(jniDemo.changeString("abcd"));
   }  
}

3 利用javac生成.class类文件

root@52acd6c79e97:/home/huxiang_j/work/jni/TestNativeCode# javac src/com/wwj/nativecode/TestNativeCode.java -d bin

4 利用javah生成.h头文件(找不到类的错误可以参考我前一篇博客)

root@52acd6c79e97:/home/huxiang_j/work/jni/TestNativeCode/bin# javah com.wwj.nativecode.TestNativeCode     

5 编译.so文件

编写source.cpp

#include<iostream>
#include<string>
#include"com_wwj_nativecode_TestNativeCode.h"

using namespace std;

JNIEXPORT void JNICALL Java_com_wwj_nativecode_TestNativeCode_sayHello
  (JNIEnv *env, jobject jj){
    cout<<"hello world"<<endl;
};
JNIEXPORT jstring JNICALL Java_com_wwj_nativecode_TestNativeCode_changeString
  (JNIEnv *env, jobject jj, jstring js){
    const char * str_in = env->GetStringUTFChars(js,false);
    string tem1,tem2;
    tem1 = str_in;
    tem2.assign(tem1.rbegin(),tem1.rend());
    cout<<"tem1:"<<tem1<<" tem2:"<<tem2<<endl;
    const char * str_out = tem2.c_str();
    jstring rt =  env->NewStringUTF(str_out);
    return rt;
};

然后利用g++编译成.so动态库

g++ -I$JAVA_HOME/include/ -I$JAVA_HOME/include/linux -shared -fPIC -o libmyjni.so source.cpp

并将这个so文件放到类的目录,这里即为bin目录,并改名为nativeCode,供后面java调用。
6 将.so添加到java.library.path环境变量中(根据自己情况修改)

export LD_LIBRARY_PATH=/home/huxiang_j/work/jni/TestNativeCode/bin/
root@52acd6c79e97:/home/huxiang_j/work/jni/TestNativeCode/bin# export LD_LIBRARY_PATH=/home/huxiang_j/work/jni/TestNativeCode/bin/

How to add .so file to the java.library.path in Linux
http://stackoverflow.com/questions/16227045/how-to-add-so-file-to-the-java-library-path-in-linux

未添加会报错,错误如下:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no nativeCode in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    at java.lang.System.loadLibrary(System.java:1122)
    at com.wwj.nativecode.TestNativeCode.main(TestNativeCode.java:8)

7 运行结果

root@52acd6c79e97:/home/huxiang_j/work/jni/TestNativeCode/bin# java com/wwj/nativecode/TestNativeCode
hello world
tem1:abcd tem2:dcba
dcba

猜你喜欢

转载自blog.csdn.net/qq_16949707/article/details/61195348