https://github.com/zq2599/blog_demos
内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;
今天咱们先写C++函数,再写Java类,该Java类用JavaCPP调用C++函数;
提前小结JavaCPP开发的基本步骤如下图,稍后就按这些步骤去做:
聪明的您应该会想到:入门demo,JavaCPP官方也有啊(https://github.com/bytedeco/javacpp),难道欣宸还能比官方的好?
官方的入门demo一定是最好的,这个毋庸置疑,我这里与官方的不同之处,是添加了下面这些官方没提到的内容,更符合自己的开发习惯(官方没有这些的原因,我觉得应该是更关注JavaCPP本身,而不是一些其他的细枝末节):
Warning: Could not load Loader: java.lang.UnsatisfiedLinkError: no jnijavacpp in java.library.path
CSDN(不用积分):https://download.csdn.net/download/boling_cavalry/20189692
GitHub:https://raw.githubusercontent.com/zq2599/blog_download_files/master/files/javacpp-project.tar
#include "NativeLibrary.h" namespace NativeLibrary { int MyFunc::add(int a, int b) { return a + b; } }
#include<iostream> namespace NativeLibrary { class MyFunc{ public: MyFunc(){}; ~MyFunc(){}; int add(int a, int b); }; }
#include<iostream> #include"NativeLibrary.h" using namespace NativeLibrary; int main(){ MyFunc myFunc; int value = myFunc.add(1, 2); std::cout << "add value " << value << std::endl; return 0; }
g++ -std=c++11 -fPIC -shared NativeLibrary.cpp -o libMyFunc.so
g++ test.cpp -o test ./libMyFunc.so
root@docker:~/javacpp/cpp# ./test add value 3
package com.bolingcavalry.javacppdemo; import org.bytedeco.javacpp.*; import org.bytedeco.javacpp.annotation.*; @Platform(include="NativeLibrary.h",link="MyFunc") @Namespace("NativeLibrary") public class Test { public static class MyFunc extends Pointer { static { Loader.load(); } public MyFunc() { allocate(); } private native void allocate(); // to call add functions public native int add(int a, int b); } public static void main(String[] args) { MyFunc myFunc = new MyFunc(); System.out.println(myFunc .add(111,222)); } }
javac -cp javacpp-1.5.5.jar com/bolingcavalry/javacppdemo/Test.java
java \ -jar javacpp-1.5.5.jar \ com/bolingcavalry/javacppdemo/Test.java
root@docker:~/javacpp/java# java \ > -jar javacpp-1.5.5.jar \ > com/bolingcavalry/javacppdemo/Test.java Info: javac -cp javacpp-1.5.5.jar:/root/javacpp/java com/bolingcavalry/javacppdemo/Test.java Info: Generating /root/javacpp/java/jnijavacpp.cpp Info: Generating /root/javacpp/java/com/bolingcavalry/javacppdemo/jniTest.cpp Info: Compiling /root/javacpp/java/com/bolingcavalry/javacppdemo/linux-x86_64/libjniTest.so Info: g++ -I/usr/lib/jvm/jdk1.8.0_291/include -I/usr/lib/jvm/jdk1.8.0_291/include/linux /root/javacpp/java/com/bolingcavalry/javacppdemo/jniTest.cpp /root/javacpp/java/jnijavacpp.cpp -march=x86-64 -m64 -O3 -s -Wl,-rpath,$ORIGIN/ -Wl,-z,noexecstack -Wl,-Bsymbolic -Wall -fPIC -pthread -shared -o libjniTest.so -lMyFunc Info: Deleting /root/javacpp/java/com/bolingcavalry/javacppdemo/jniTest.cpp Info: Deleting /root/javacpp/java/jnijavacpp.cpp
此时的com/bolingcavalry/javacppdemo目录下新增了一个名为linux-x86_64的文件夹,里面的libjniTest.so是javacpp-1.5.5.jar生成的
您可以将/usr/lib/目录下的libMyFunc.so文件移动到linux-x86_64目录下(不移动也可以,只是个人觉得业务so文件放在/usr/lib/这种公共目录下不太合适)
将java应用运行起来:
java -cp javacpp-1.5.5.jar:. com.bolingcavalry.javacppdemo.Test
root@docker:~/javacpp/java# java -cp javacpp-1.5.5.jar:. com.bolingcavalry.javacppdemo.Test Warning: Could not load Loader: java.lang.UnsatisfiedLinkError: no jnijavacpp in java.library.path 333
root@docker:~# tree /root/javacpp /root/javacpp ├── cpp │ ├── libMyFunc.so │ ├── NativeLibrary.cpp │ ├── NativeLibrary.h │ ├── test │ └── test.cpp └── java ├── com │ └── bolingcavalry │ └── javacppdemo │ ├── linux-x86_64 │ │ ├── libjniTest.so │ │ └── libMyFunc.so │ ├── NativeLibrary.h │ ├── Test.class │ ├── Test.java │ └── Test$MyFunc.class └── javacpp-1.5.5.jar 6 directories, 12 files
root@docker:~/javacpp/java# java -cp javacpp-1.5.5.jar:. com.bolingcavalry.javacppdemo.Test Warning: Could not load Loader: java.lang.UnsatisfiedLinkError: no jnijavacpp in java.library.path 333
上述告警信息不会影响功能,如果想消除掉,就不能只用org.bytedeco:javacpp:1.5.5这一个库,而是org.bytedeco:javacpp-platform:1.5.5,以及它的依赖库
由于本篇没有用到maven或者gradle,因此很难将org.bytedeco:javacpp-platform:1.5.5及其依赖库集齐,我这里已经将所有jar文件打包上传,您可以选择下面任意一种方式下载:
下载下来后解压,是个名为lib的文件夹,将此文件夹放在/root/javacpp/java/目录下
lib文件夹下的jar只是在运行时用到,编译时用不上,因此现在可以再次运行java应用了,命令如下:
java -cp lib/*:. com.bolingcavalry.javacppdemo.Test
root@docker:~/javacpp# tree /root/javacpp /root/javacpp ├── cpp │ ├── libMyFunc.so │ ├── NativeLibrary.cpp │ ├── NativeLibrary.h │ ├── test │ └── test.cpp └── java ├── com │ └── bolingcavalry │ └── javacppdemo │ ├── linux-x86_64 │ │ ├── libjniTest.so │ │ └── libMyFunc.so │ ├── NativeLibrary.h │ ├── Test.class │ ├── Test.java │ └── Test$MyFunc.class ├── javacpp-1.5.5.jar └── lib ├── javacpp-1.5.5-android-arm64.jar ├── javacpp-1.5.5-android-arm.jar ├── javacpp-1.5.5-android-x86_64.jar ├── javacpp-1.5.5-android-x86.jar ├── javacpp-1.5.5-ios-arm64.jar ├── javacpp-1.5.5-ios-x86_64.jar ├── javacpp-1.5.5.jar ├── javacpp-1.5.5-linux-arm64.jar ├── javacpp-1.5.5-linux-armhf.jar ├── javacpp-1.5.5-linux-ppc64le.jar ├── javacpp-1.5.5-linux-x86_64.jar ├── javacpp-1.5.5-linux-x86.jar ├── javacpp-1.5.5-macosx-arm64.jar ├── javacpp-1.5.5-macosx-x86_64.jar ├── javacpp-1.5.5-windows-x86_64.jar ├── javacpp-1.5.5-windows-x86.jar └── javacpp-platform-1.5.5.jar 7 directories, 29 files
微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界...
https://github.com/zq2599/blog_demos