JAVA调用c++编写的dll总结

方法: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;

后续再补充

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java调用C语言主要是通过JNI(Java Native Interface)来实现的。JNI是一种Java应用程序与本地代码(如C或C++)进行交互的机制。 首先,需要编写一个C语言的共享库(也称为动态链接库或DLL)。在C代码中,使用`#include <jni.h>`包含JNI的头文件,定义一个`JNIEXPORT`修饰的方法,该方法会在Java中被调用。在这个方法中,可以执行C语言的操作,或者通过调用Java提供的JNI函数来与Java进行交互。 编写完C代码后,需要使用编译器将其编译成共享库文件。在Windows环境下,可以使用命令行运行`gcc -shared -o libexample.dll example.c`命令进行编译。在Linux环境下,可以使用`gcc -shared -o libexample.so example.c`命令进行编译。 在Java代码中,使用`System.loadLibrary()`方法加载已编译的共享库文件。然后,通过`native`关键字将Java方法声明为本地方法。在方法体内部,可以调用C语言的方法。 例如,假设C代码中定义了一个`JNIEXPORT void JNICALL Java_com_example_NativeExample_nativeMethod(JNIEnv *env, jobject obj)`方法,在Java中可以通过以下代码调用该方法: ``` class NativeExample { public native void nativeMethod(); static { System.loadLibrary("example"); } } public class Main { public static void main(String[] args) { NativeExample example = new NativeExample(); example.nativeMethod(); } } ``` 需要注意的是,调用C代码时需要处理好Java和C之间的类型转换和内存管理。JNI提供了一些函数来实现这些功能,如`GetMethodID()`、`CallVoidMethod()`和`GetArrayElements()`等。 总之,Java通过JNI调用C语言主要是通过编写C共享库,并使用JNI函数来与Java进行交互。通过合理使用JNI提供的函数,可以在Java调用C代码并实现更高效的程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值