java调用dll

时间:2023-03-09 00:15:19
java调用dll

@参考文章1@参考文章2

根据上篇博客(参考文章2)java生成的dll测试

1,新建java项目,新建WebContent,子目录建WEB-INF\lib,加进jna-3.4.0.jar

新建java类调用dll

import com.sun.jna.*;

public class CallDll {
public static void main(String[] args) {
try {
//调用dll中的方法
CLibrary.sdtapi.Java_Hello_printHello(); } catch (Exception e) {
e.printStackTrace();
}
}
} interface CLibrary extends Library {
//dll绝对路径的地址获取,注意要去空格,特别坑
String filePath = CLibrary.class.getResource("").getPath().replaceFirst("/","").replaceAll("%20"," ")+"HelloWorld.dll";
CLibrary sdtapi = (CLibrary) Native.loadLibrary(filePath, CLibrary.class);
//动态链接库中的方法
void Java_Hello_printHello();
//==============================================dll public method e
}

3,将HelloWorld.dll加入项目根目录下

4,运行main,结果如下

java调用dll

5,项目打包下载:callDll.rar