JNA 实际开发中若干问题解决方法
JNA 实际开发中若干问题解决方法
很早就已经听说过 JNI(Java Native Interface)Java 本地接口,奈何调用本地动态链接库太过于复杂,就一直没有再尝试。而最近因为工作需要调用 Windows DLL 动态链接库(对应 Linux 中 so 文件),而对 JNA 有了入坑体验,对实际工作中遇到的问题做出总结。
1. 调用 Windows 窗口打印Hello World
- pom 依赖
<dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna</artifactId> <version>latest</version> </dependency>
- JNA 在加载驱动时提供两种加载方式 , 直接映射 和 接口生成。
接口生成
public interface HelixcsDll extends StdCallLibrary { // loadLibary 为动态链接库加载目录 HelixcsDll HELIXCS_DLL = Native.loadLibrary("helixcs.dll", HelixcsDll.class); // 在 dll 中存在相同 SomeFunction 的函数 void SomeFunction(String content); } // 调用 HelixcsDll.HELIXCS_DLL.SomeFunction("Hello World");
直接映射
class Helixcs{ static { Native.register("helixcs.dll"); } // 映射为本地方法 public static native void SomeFunction(String content); public static void main(String[] args) { SomeFunction("Hello World"); } }
2. C 类型和 Java 类型映射。
- JNA 官方提供的默认类型映射
Default Type Mappings 默认类型映射
Java primitive types (and their object equivalents) map directly to the native C type of the same size.
Java 原始类型以相同的大小映射 C 类型。
char | 8-bit integer | byte | BYTE, TCHAR |
short | 16-bit integer | short | WORD |
wchar_t | 16/32-bit character | char | TCHAR |
int | 32-bit integer | int | DWORD |
int | boolean value | boolean | BOOL |
long | 32/64-bit integer | NativeLong | LONG |
long long | 64-bit integer | long | __int64 |
float | 32-bit FP | float | |
double | 64-bit FP | double | |
char* | C string | String | LPTCSTR |
void* | pointer | Pointer | LPVOID, HANDLE, LPXXX |
未签名类型作为签名类型来映射。 C 中枚举类型可替换为 “int”。
Unsigned types use the same mappings as signed types. C enums are usually interchangeable with "int".
3. 官方提供的详细的类型映射
Marshalling/Unmarshalling (Java/Native Type Conversions)
C Type | Native Representation | Java Type |
char | 8-bit integer | byte |
wchar_t | platform-dependent | char |
short | 16-bit integer | short |
int | 32-bit integer | int |
int | boolean flag | boolean |
enum | enumeration type | int (usually) |
long long, __int64 | 64-bit integer | long |
float | 32-bit floating point | float |
double | 64-bit floating point | double |
pointer (e.g. void*) | platform-dependent (32- or 64-bit pointer to memory) | Buffer Pointer |
pointer (e.g. void*), array | 32- or 64-bit pointer to memory (argument/return) contiguous memory (struct member) | <P>[](array of primitive type) |
In addition to the above types, which are supported at the native layer, the JNA Java library automatically handles the following types. All but NativeMapped and NativeLong are converted to Pointer before being passed to the native layer. | ||
long | platform-dependent (32- or 64-bit integer) | NativeLong |
const char* | NUL-terminated array (native encoding or jna.encoding ) | String |
const wchar_t* | NUL-terminated array (unicode) | WString |
char** | NULL-terminated array of C strings | String[] |
wchar_t** | NULL-terminated array of wide C strings | WString[] |
void** | NULL-terminated array of pointers | Pointer[] |
struct* struct | pointer to struct (argument or return) (or explicitly )struct by value (member of struct) ( or explicitly ) | Structure |
union | same as Structure | Union |
struct[] | array of structs, contiguous in memory | Structure[] |
void (*FP)() | function pointer (Java or native) | Callback |
pointer (<T> *) | same as Pointer | PointerType |
other | integer type | IntegerType |
other | custom mapping, depends on definition | NativeMapped |
4. 经验总结默认映射关系
尽管 JNA 官方已经提供了详细的类型映射文档。但在实际中发现按照官方映射可能出现莫名问题。对此我们在实际开发中对于一些类型的映射报错,可以参考以下映射做出调整。
C类型 | JNA类型 | 说明 |
---|---|---|
char * out | Pointer | Pointer room = new Memory(30); |
uchar * out | Pointer | Pointer room = new Memory(30); |
long * | long [ ] | |
int * | init [ ] | |
int | int | |
char * | byte [ ] | |
char * argv[] | String [] | |
uchar | int | |
long | NativeLong | 兼容 32和64位 |
5. 常见错误
1. UnsatisfiedLinkError 问题
存在多个动态链接库之间调用情况,可能缺少其中某一个动态链接库文件。
2. Error Memory Access 问题
较大情况下存在参数类型映射错误,参考 JNA 类型映射。
3. dll can not find in win32/86
无法加载到动态链接库文件路径,需要将动态链接库放到项目根目录下,推荐开启 JNA 驱动加载 debug 模式,
System.setProperty("jna.debug_load", "true");
手动指定动态链接库文件路径
System.setProperty("jna.library.path",dllResourcePath); System.setProperty("jna.platform.library.path",dllResourcePath);
4. 程序在运行一段时间后崩溃
在JNA crash-protection 中,官方文档说明的崩溃的主要原因,( These are often caused by improper mappings or invalid arguments passed to the native library.) 未知的参数类型映射导致vm崩溃。在很大程度上使用 Pointer
来作为 Java 通用映射类型。
而 JNA 默认程序保护Native.setProtected(true)
使得 java 错误来代替程序崩溃,可以设置 Native.setProtected(false)
来 dump 出崩溃日志。
问题可以反馈到 JNA Google Group : https://groups.google.com/forum/#!forum/jna-users
6. 参考
- JNA Google Group : https://groups.google.com/forum/#!forum/jna-users
- JNA API Documentation: https://java-native-access.github.io/jna/4.2.1/overview-summary.html
- JNA VM Crashed Protection :http://java-native-access.github.io/jna/4.5.1/javadoc/overview-summary.html#crash-protection
原文来自:https://iliangqunru.bitcron.com/post/2018/jna-shi-ji-kai-fa-zhong-ruo-gan-wen-ti-jie-jue-fang-fa
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
java的对象与垃圾回收机制
java 的垃圾回收是 java 语言的重要功能之一。当程序创建对象、数组等引用类型实体时,系统会在堆内存中位置分配一块内存区,对象就保存在这块内存区中,当这块内存不在被任何变量引用时,这块内存就变成垃圾,等待垃圾回收机制进行回收。垃圾回收机制具有如下特征。 垃圾回收机制只负责回收堆内存中的对象,不会回收任何物理资源(例如数据库连接,网络等资源) 程序无法控制垃圾回收的运行,垃圾回收会在合适的时候进行。当对象永久性的失去引用后,系统会在合适的时候回收它占用的内存。 在垃圾回收机制回收任何对象之前,总会先调用它的finalize()方法,该方法可能会使该对象重新复活(让一个引用变量引用该对象),从而导致垃圾回收机制取消回收。 对象在内存中的状态 当一个对象在堆内存中运行时,根据它被引用变量引用的状态,可以把它所处的状态分成如下三种。 可达状态:当一个对象被创建后,若有一个以上的引用变量引用它,则这个对象在程序中处于可达状态,程序可通过引用变量来调用该对象的实例变量和方法。 可恢复状态:如果程序中某个对象不再有任何变量引用它,它就进入了可恢复状态。在这种状态下,系统的垃圾回收机制准备回收...
- 下一篇
基于 Python 自建分布式高并发 RPC 服务
RPC(Remote Procedure Call)服务,也即远程过程调用,在互联网企业技术架构中占据了举足轻重的地位,尤其在当下微服务化逐步成为大中型分布式系统架构的主流背景下,RPC 更扮演了重要角色。 Google 开源了 gRPC,Facebook 开源了 Thrift,Twitter 开源了 Finagle,百度开源了 bRPC,腾讯开源了 Tars,阿里开源了 Dubbo 和 HSF,新浪开源了 Motan 等,一线互联网大厂们纷纷亮出自己研制的 RPC 框架武器,在解决分布式高并发业务问题的同时,也向外界展示自己的技术实力。 互联网公司百花齐放,貌似高深复杂的 RPC 框架解决了哪些业务难题?其技术含量究竟高在哪里?后端开发者该如何深入掌握 RPC 开发?这些都是本小册想要解读的问题。 分布式高并发服务是只有高手才能涉足的领域 虽然大部分后端开发者在日常开发中都会隐式或显式的使用 RPC,但对初级开发者来说 RPC 总是略显神秘而有距离,而即便有多年 RPC 使用经验的程序员,也可能依然对 RPC 背后的原理不甚了解,难以精通,遇到复杂问题时难免误用。 RPC 本身理论繁...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
-
Docker使用Oracle官方镜像安装(12C,18C,19C)
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS8编译安装MySQL8.0.19
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS7,8上快速安装Gitea,搭建Git服务器
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
推荐阅读
最新文章
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS6,CentOS7官方镜像安装Oracle11G
- SpringBoot2整合Redis,开启缓存,提高访问速度
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Hadoop3单机部署,实现最简伪集群
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果