使用JNI在Java里面使用C++

基本来自以下两篇文:https://blog.csdn.net/lj402159806/article/details/86169807

https://blog.csdn.net/qinjuning/article/details/7595104

尝试将一个C++中的vector容器转化为java中的arraylist返回

编译jni动态库

    1. 创建文件夹
      1
      2
      3
      4
      #创建java层文件夹,用来存放你的java文件
      mkdir -p java/com/example/jni
      #创建native层文件夹,用来存放cpp等的文件
      mkdir native
    1. 创建java文件
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      echo "package com.example.jni;

      import java.util.List;
      import java.util.ArrayList;

      public class NativeLib {
      static {
      System.loadLibrary(\"NativeLib\");
      }

      public static void main(String[] args) {
      ArrayList<Integer> list = getCppVector();
      for(Integer attribute : list) {
      System.out.println(attribute);
      }
      }

      public static native ArrayList<Integer> getCppVector();
      }
      }" > java/com/example/jni/NativeLib.java

    1. 生成class文件和jni头文件,并把头文件移动到native文件夹内。
      1
      2
      3
      4
      5
      cd java
      javac com/example/jni/NativeLib.java
      javac -h . com/example/jni/NativeLib.java
      mv com_example_jni_NativeLib.h ../native/NativeLib.h
      cd ../native
    1. 创建NativeLib.cpp文件,并写入以下内容。
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      #include "NativeLib.h"
      #include <iostream>
      #include <ctime>
      #include <cstdlib>
      #include <vector>

      std::vector<int> getRandomIntVector(){
      unsigned _size = rand()%100 + 10;
      srand(time(nullptr));
      std::vector<int> _Ans;
      for (unsigned i = 0 ; i < _size ; ++i)
      _Ans.push_back(rand());
      return _Ans;
      }

      jobject stdvectorint_to_ArrayListInteger(JNIEnv *env, const std::vector<int> &_vec) {
      //获取ArrayList类
      jclass _clazzArrayList = env->FindClass("java/util/ArrayList");
      //获取ArrayList的默认构造函数(无参)
      jmethodID _clazzArrayList_Constructor = env->GetMethodID(_clazzArrayList, "<init>", "()V");
      //使用构造函数构造一个新的ArrayList的对象_Ans
      jobject _Ans = env->NewObject(_clazzArrayList, _clazzArrayList_Constructor);
      //获取ArrayList对象的方法add
      jmethodID _clazzArrayList_add = env->GetMethodID(_clazzArrayList, "add", "(Ljava/lang/Object;)Z");
      unsigned _size = _vec.size();
      for (unsigned i = 0 ; i < _size ; ++i) {
      //获取Integer类
      jclass _clazzInteger = env->FindClass("java/lang/Integer");
      //获取Integer的构造函数,构造函数的类型为Integer(int)
      jmethodID _clazzIntegerConstructor = env->GetMethodID(_clazzInteger, "<init>", "(I)V");
      //使用构造函数构造一个新的Integer对象_Tmp
      jobject _Tmp = env->NewObject(_clazzInteger, _clazzIntegerConstructor, _vec[i]);
      /*分配Integer对象,不调用构造函数
      jobject _Tmp = env->AllocObject(_clazzInteger);
      调用_Tmp的构造函数
      env->CallNonvirtualVoidMethod(_Tmp, _clazzInteger, _clazzIntegerConstructor, _vec[i]);*/
      //调用_Ans的add方法
      env->CallObjectMethod(_Ans, _clazzArrayList_add, _Tmp);
      }
      return _Ans;
      }

      JNIEXPORT jobject JNICALL Java_com_example_jni_NativeLib_getCppVector
      (JNIEnv *env, jclass type) {
      return stdvectorint_to_ArrayListInteger(env, getRandomIntVector());
      }
    1. 创建CMakeLists.txt文件
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      echo "cmake_minimum_required(VERSION 2.8)

      #c++11支持
      add_definitions(-std=c++11)

      #设置java路径
      set(java_home \"/usr/lib/jvm/default\")

      #jni头文件
      include_directories(\${java_home}/include/
      \${java_home}/include/linux/)

      add_library(NativeLib SHARED
      NativeLib.cpp)

      target_link_libraries(NativeLib)" > ../native/CMakeLists.txt
注意:这里java_home需要替换为你自己的java安装路径

如何科学的找到自己的java安装路径?

    1. 编译
      1
      2
      3
      4
      5
      6
      7
      #创建build文件夹并进入
      mkdir build
      cd build
      #生成预编译文件
      cmake -DCMAKE_BUILD_TYPE=Release ..
      #编译
      cmake --build .
      编译成功的话,build目录下会生成libNativeLib.so文件,将该文件复制到java目录内。
      1
      cp libNativeLib.so ../../java
      测试so动态库
      1
      2
      3
      cd ../../java
      #运行class文件
      java -Djava.library.path=. com.example.jni.NativeLib

如果出现一堆随机数,那么恭喜你成功了。

作者

uchkks

发布于

2019-05-18

更新于

2019-11-26

许可协议