JAVA调用C++遇到的坑

1 篇文章 0 订阅

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 = ""
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值