@StarSky
2018-08-20T10:21:12.000000Z
字数 2627
阅读 1984
工作日记
.jar file[2].dylib or .so file.[3]java.lib.path [5][6], and put the .dylib or .so in.edit CmakeList.txt add .h & .cpp as entrance
ADD_LIBRARY(projectName SHARED include/jni/com_****_JNI.h src/jni/****JNI.cpp)
@Q: when using on the linux service, the third method doesn't work, also the other[8], we should notice the difference between osx and linux[9].
@Q: while cmake the project
/export/home/aiusr/env/image/bin/ld:src/jni/libJNI.a(IdcardRecognizerController.cpp.o): relocation R_X86_64_32 against '.bss' can not be used when making a shared object; recompile with -fPIC
error occured
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
need to be modified to
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fPIC ")
It seems like linux enviroment is the main reason.
public class JNIService {private static final JNIService instance = new JNIService();private JNIService(){try{System.out.println("----start-----");System.out.println(System.getProperty("java.library.path"));System.out.println("----end-----");System.loadLibrary("cppLib");}catch(Throwable e){System.out.println("The cppLib don't load correctly");}}public static JNIService getInstance(){return instance;}//define a native functionpublic native String function(int type, String filePath);public String callCpp(Map<String, Object> paramMap) throws Exception {String res = function(type, filePath);System.out.print("res is ------->>>>>>" + res);return res;}}
singleton, new the service will be illegal, so I used the JNIService.getInstance().
public class TestJNI {private JNIService jNIService = JNIService.getInstance();@Testpublic void test(){Map<String,Object> paramMap = new HashMap<String,Object>();paramMap.put("type","front");paramMap.put("imgFilePath","/Users/saber/Downloads/test_img/51152119960731792x_1.jpeg");try {String res = JNIService.callCpp(paramMap);} catch (Exception e) {e.printStackTrace();}}}
cd build > cmake .. > make you will get a dylib (sure you have a correct CmakeList) ↩echo $LD_LIBRARY_PATH command or println(System.getProperty("java.library.path")) in java can show the "java.library.path" ↩