1、导入JNA包:
<dependency>
<groupId>com.sun.jna</groupId>
<artifactId>jna</artifactId>
<version>3.0.9</version>
</dependency>
2、写一个接口CEnDeCheckLib继承Library类,此接口中即可撰写C++动态库的方法。
3、使用下述方法加载动态库的实例:libraryPath为动态库中.dll文件(windows平台下的使用的动态库文件)或.so(linux平台下使用的动态库文件)文件存储的位置
CEnDeCheckLib instance = (CEnDeCheckLib) Native.loadLibrary(libraryPath, CEnDeCheckLib.class);
4、使用instance 调用动态库的方法。
注意:调用动态库传参时有坑:
java中一般字符串类型设置为String,当属性无值时,默认为null,如果把null传给动态库方法,idea运行项目会突然停止,且报出如下错误:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007fff4371669a, pid=4432, tid=0x0000000000003a64
#
# JRE version: Java(TM) SE Runtime Environment (8.0_191-b12) (build 1.8.0_191-b12)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.191-b12 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [endecheck.dll+0x3669a]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# E:\work\git\gis-lss-map-license-management-80003819\app-license-parent\hs_err_pid4432.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
传给动态库方法的字符串参数如无值时,需传入空的字符串"",不能传入null,为解决此问题,可将java的String属性对应的默认值修改为"",如
String result = ""