当前位置:网站首页>JNA loading DLL and its application in jar
JNA loading DLL and its application in jar
2022-07-19 05:40:00 【Spring and autumn of Qin and Han Dynasties】
List of articles
Demand related
- demand
Use JNI Mode load DLL, And encapsulated as SDK For others . - Method
- Native JNI The way
- call JNA frame
- Native JNI The method is suitable for customized dll, That is, having dll In the case of source code , Strong operability ; However, if there are too many methods and structures involved , You need to write many type conversions by yourself
- JNA It can be used in all kinds of scenes , It saves effort in type conversion , It's necessary for lazy people
This time, the target is a header file windows32 position dll file , So choose to use JNA.
Be careful , the reason being that win32 platform , So it's used throughout 32 Bit JDK, Used here jdk_1.8.152_x86.
DLL relevant
Of course, in order to step on the pit, if conditions permit, it is still self created dll To try . So I choose to sacrifice Universe first IDE, Kill a chicken with an ox knife — Generate a simple dll file .
The header file :
#pragma once
#define DLL_API _declspec(dllexport)
extern "C" {
DLL_API int getVersion(void);
DLL_API int add(int a, int b);
}
Source code :
#include "pch.h"
#include "soft.h"
int getVersion(void) {
return 2;
}
int add(int a, int b) {
return a + b;
}
Next, naturally, is generation DLL. Here are some configurations to pay attention to :
- Use Release/win32 To generate solutions
- choice “ Use... In a static library MFC”
It can be configured as shown in the figure :
If not configured like this , There will be Can’t find dependent libraries problem , It means the current dll Rely on others dll, But I can't find it in the current environment . Specific dependencies can be used dependencywalker Conduct detection query , A situation similar to the following figure shows the lack of dependency :
The configuration. , You can pick up a fresh one dll file .
good , Now let's simulate a situation — The source code is missing . This becomes the requirement mentioned at the beginning .
JNA relevant
JNA It's a mature framework , Of course, it's organized . All heroes can be in github Get the latest version on , The portal is here :
jna
The latest version is 5.5.0. Here is also a direct report of its jar Of maven Address :
jna-5.5.0
jna-platform-5.5.0
Two bags , The former is equivalent to the kernel package , The latter is similar to the plug-in package . Generally, you can download the former .
then …
You'll find this jar The bag is really big .
Now it's 1.4M size , And we generated in the previous step Dll File size :
Kill chicken with ox knife X 2.
This can't be tolerated , You have to cut it .
By opening the jar Package we can see :
This is a compatible package for various environments , All we need here is win32 Bit , We have to find a way Get the source code and delete it , Regenerate another jna Of jar Wrap it up .
However, in maven The provision of the jna-5.5.0-sources.jar These platform related things are not in the source package , So you can only close it manually . Fortunately, the platform related compatibility packages here are not class file , It is Various types so file 、dll file 、jnilib file etc. . So you can close it directly .
Specific operation :
- hold jna-5.5.0-sources.jar Copy all the code in the source package
- hold jna-5.5.0.jar Copy the required environment compatibility package under the directory
- Generate jar package
stay IDEA It's probably like this in the middle of the story :
All we need here is win32-x86 Environmental support for , So only the compatible packages of this environment are copied , You can see , Just add jnidispatch.dll File can .
Then configure artifacts, Fast generation jar package :
So I found a size 288KB Only supported win32-x86 Of jna package :
Although it's still a little big , But let's get together first .
Use JNA load DLL
First test it in the external project . First, the previously generated SimpleDll.dll Files in Project root Next , Then just write an inheritance Libary The interface of , load dll And the statement dll The method in :
import com.sun.jna.Library;
import com.sun.jna.Native;
public interface ISimpleDll extends Library {
ISimpleDll INSTANCE = Native.loadLibrary("SimpleDll", ISimpleDll.class);
int getVersion();
int add(int a, int b);
}
Finally, you can call :
public class Main {
public static void main(String[] args) {
System.out.println(ISimpleDll.INSTANCE.getVersion());
System.out.println(ISimpleDll.INSTANCE.add(1,2));
}
}
Output :
2
3
Its directory structure is as follows :
So far JNA Bao He DLL All files are normal .
stay jar Use in JNA load DLL
stay jar The package uses JNA load dll, The biggest problem is actually routing problem , That is to say How to make jna stay jar The package can still find the corresponding dll file .
take SimpleDll Files in Libary Module Root directory , And hit jar package :
Then prompt when using :
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'SimpleDll':
The specified module was not found .
The specified module was not found .
The specified module was not found .
Can't obtain InputStream for win32-x86/SimpleDll.dll
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:302)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:455)
at com.sun.jna.Library$Handler.<init>(Library.java:192)
at com.sun.jna.Native.loadLibrary(Native.java:646)
at com.sun.jna.Native.loadLibrary(Native.java:630)
at SomeSDK.<init>(SomeSDK.java:16)
at SomeSDK.<init>(SomeSDK.java:3)
at SomeSDK$Holder.<clinit>(SomeSDK.java:6)
at SomeSDK.getInstance(SomeSDK.java:10)
at Main.main(Main.java:4)
Seems to want win32-x86 Go down to the directory and find this dll file , That's easy —
Change it artifacts Configuration of , Just add a directory manually :
Sure enough .
New version of the JNA It seems that some changes have been made in the search on the path .
Last , all JAVA Upload the code to github Yes .
JnaDemo
Last, last , Again , Used throughout this commissioning win32 Of dll, as well as 32 Bit JDK.
边栏推荐
- Syntax differences between PgSQL and Oracle (SQL migration records)
- OpenCV读取中文路径下的图片,并对其格式转化不改变颜色
- Flutter Intl的使用
- JNI实用笔记
- Calculator of wechat applet
- C language dynamic memory management
- Pointer array & array pointer
- 【语音识别入门】基础概念与框架
- 5.1 business data acquisition channel construction of data acquisition channel construction
- 微信小程序的常用组件
猜你喜欢

电商用户行为实时分析系统(Flink1.10.1)

尝试解决YOLOv5推理rtsp有延迟的一些方法

Minor problems of GCC compiling C language in ubantu

Solve idea new module prompt module XXXX does exits

Use Flink SQL to transfer market data 1: transfer VWAP

软件过程与管理复习(十)

2.东软跨境电商数仓项目技术选型

E-commerce user behavior real-time analysis system (flink1.10.1)

4. Neusoft cross border e-commerce data warehouse project - user behavior data acquisition channel construction of data acquisition channel construction (2022.6.1-2022.6.4)

10. DWD layer construction of data warehouse construction
随机推荐
Custom components of wechat applet
运行基于MindSpore的yolov5流程记录
正则替换group(n)内容
Scala初级实践——统计手机耗费流量(1)
E-commerce user behavior real-time analysis system (flink1.10.1)
微信小程序的常用組件
cuda11.0的pytorch安装小计
mysql的事务
软件过程与管理总复习
PCM静默检测
MySQL basic grammar dictionary
Ambari cluster expansion node + expansion service operation
Wxml template syntax in wechat applet
MySQL--触发器与视图
3.东软跨境电商数仓项目架构设计
gradle
Scala primary practice - statistics of mobile phone traffic consumption (1)
安卓实现真正安全的退出app
【Bug解决】org.apache.ibatis.type.TypeException: The alias ‘xxxx‘ is already mapped to the value ‘xxx‘
跨域和处理跨域