• docker安装CentOS7及JNI使用相关过程记录


    docker pull centos:centos7(拉取镜像)

    docker run -itd --name centos-test centos:centos7 (运行容器)

    docker exec -it centos-test /bin/bash (进入容器)

    举例使用JNI调用c++ googleS2:

    (安装编译c++版本googlS2依赖)

    yum -y install cmake libgflags-dev libgoogle-glog-dev libgtest-dev libssl-dev(其中yum源应该是没有libssl-dev这个包)

     

    (拷贝预先下载的本地文件到centos:

    docker ps(查看容器名字centos-test)

    docker inspect -f '{{.Id}}' centos-test(根据名字获得CONTAINER ID 完整ID:87880fa28f280a5a01712d4104d8ed31700363b10dd784aaafbb6abc6428aad5)

    docker cp /本机绝对路径/googletest-release-1.8.0.tar.gz 87880fa28f280a5a01712d4104d8ed31700363b10dd784aaafbb6abc6428aad5:/root/googletest-release-1.8.0.tar.gz

    )

    tar xvzf googletest-release-1.8.0.tar.gz 

     

    mkdir googleS2

    cd googleS2/

    (安装git:

    yum install git

    git config --global user.name “名字”

    git config --global user.email 邮箱

     

    git clone https://github.com/google/s2geometry.git

    cd s2geometry/

    mkdir build

    cd build/

    cmake -DGTEST_ROOT=/root/googletest-release-1.8.0/googletest/ ..

    (出错,原因cmake版本太低,升级:

    cd /opt/

    (安装wget

    yum -y install wget

    wget https://cmake.org/files/v3.10/cmake-3.10.2-Linux-x86_64.tar.gz

    yum remove cmake

    tar zxvf cmake-3.10.2-Linux-x86_64.tar.gz

    vi /etc/profile.d/cmake.sh

    写入:

    export CMAKE_HOME=/opt/cmake-3.10.2-Linux-x86_64

    export PATH=$PATH:$CMAKE_HOME/bin

    保存退出

    source /etc/profile

    cmake -version

    可查看到最新的cmake版本

    cd /root/googleS2/s2geometry/build/

    cmake -DGTEST_ROOT=/root/googletest-release-1.8.0/googletest ..

    (缺少g++编译器,安装:

     yum install gcc

     yum install gcc-c++

     yum install make

    )

    cmake -DGTEST_ROOT=/root/googletest-release-1.8.0/googletest ..

    (缺少openssl安装:

    yum install openssl

    yum install openssl-devel

    cmake -DGTEST_ROOT=/root/googletest-release-1.8.0/googletest ..

    make

    make test  

    sudo make install

     

    centos7安装java1.8

    yum install java-1.8.0-openjdk

    yum install java-devel

    update-alternatives --config java

     

    之后就可以进行正常的JNI开发了。

    vi LinuxJNI.java

    public class LinuxJNI{
         public native void sayHello();
    
            static {
                    System.load("/绝对路径/libs2.so");//当libs2.so变更存放位置时需要明确指定,默认安装位置是/usr/local/lib/libs2.so,可以不指定
                    System.load("/root/googleS2/s2geometry/src/liblinuxjni.so");
            }
            public static void main(String[] args){
                    System.out.println("hello world");
                    LinuxJNI lj = new LinuxJNI();
                    lj.sayHello();
            }
    }
    

    javac LinuxJNI.java

    javah LinuxJNI(获得LinuxJNI.h

    vi LinuxJNI.cc

    #include <iostream>
    #include "LinuxJNI.h"
    #include "s2/s2polyline.h"
    #include "s2/s2latlng.h"
    #include <vector>
    
    using namespace std;
    
    JNIEXPORT void JNICALL Java_LinuxJNI_sayHello (JNIEnv *, jobject){
            cout << " hello from jni" << endl;
    
            S2LatLng latlng_one = S2LatLng::FromDegrees(39.965377,116.392767);
            S2LatLng latlng_two = S2LatLng::FromDegrees(39.968699,116.400642);
            S2LatLng latlng_three = S2LatLng::FromDegrees(39.968929,116.407616);
    
            S2LatLng line[] = {latlng_one,latlng_two,latlng_three};
            const vector<S2LatLng> s2line(line,line+3);
            S2Polyline s2Polyline = S2Polyline(s2line);
    
            //S2Polyline 相关方法使用
         ...... }

    g++ -fPIC -D_REENTRANT -I/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252.b09-2.el7_8.x86_64/include -I/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252.b09-2.el7_8.x86_64/include/linux/ -std=c++11 -c LinuxJNI.cc

    g++ -shared -o liblinuxjni.so LinuxJNI.o /usr/local/lib/libs2.so 

    objdump -x liblinuxjni.so | grep NEEDED (检查是否依赖libs2.so成功)

    java LinuxJNI 查看运行结果。

     

  • 相关阅读:
    同步ajax请求
    Thinking in Java——笔记(11)
    Thinking in Java——笔记(10)
    Thinking in Java——笔记(9)
    Thinking in Java——笔记(8)
    Thinking in Java——笔记(7)
    Thinking in Java——笔记(6)
    Thinking in Java——笔记(5)
    Thinking in Java——笔记(4)
    Thinking in Java——笔记(3)
  • 原文地址:https://www.cnblogs.com/Carrie-0173/p/12906017.html
Copyright © 2020-2023  润新知