方法:JNI
参考:http://www.cnblogs.com/AnnieKim/archive/2012/01/01/2309567.html
1,java只需声明(public native int Add(int a, int b);)
2,javac/javah配置问题
a.配置javac系统变量时,使用绝对路径。
b.javah -classpath C:\Users\admin\Desktop\Test\src com.PathPlanNdk,也就是说,指定类的路径时,应该以包所在路径为准,而不是具体类所在的子目录
3,自己编写一个testdll实现+-*/,在编写一个Medium调用(我写在一个工程里)
Medium配置:C/C++——常规——附加包含目录——$(SolutionDir)testdll;C:\Program Files %28x86%29\Java\jdk1.8.0_181\include\win32;C:\Program Files %28x86%29\Java\jdk1.8.0_181\include;
链接器——输入:$(SolutionDir)x64\Debug\testdll.lib;
把刚才生成的com_PathPlanNdk.h copy到medium目录下,右键添加
4,编写Medium.cpp,注意#include "testdll.h"
#include "com_PathPlanNdk.h"
5,新建java类Test.java,将生成的Medium.dll和testdll.dll复制到该目录下,
(1)no XXX in java.library.path问题 我使用的绝对路径,一定要根据依赖关系依次添加dll
static {
System.out.println(System.getProperty("java.library.path"));
String curPath=System.getProperty("user.dir");
System.load(curPath+"/src/Test/libmysql.dll");
System.load(curPath+"/src/Test/mysqlcppconn.dll");
System.load(curPath+"/src/Test/dbiface.dll");
//System.load(curPath+"/src/Test/PathPlanNdk.dll");
System.load(curPath+"/src/Test/RoutingModule.dll");
System.load(curPath+"/src/Test/PathPlanNdk.dll");
/*System.loadLibrary("testdll");
System.loadLibrary("Medium");*/
}
(2)Can't find dependent libraries 如上
(3)编译java代码时,报错:找不到符号 ———————重新编译
6,java数据与c++数据转换:
(1)
string jstr2str(JNIEnv *env, jstring javaString) {
const char *nativeString = env->GetStringUTFChars(javaString, 0);
// use your string
string res = nativeString;
env->ReleaseStringUTFChars(javaString, nativeString);
return res;
}
(2)str转jstr,先转char*再转jstr
jstring jres;
string path; ...
// string 转 char*
char *ch = (char*)path.c_str();
// char* 转 string
jres = env->NewStringUTF(ch);
return jres;
后续再补充