JNA 如何 加载多个 存在依赖的 DLL 库
JNA 的出现,极大的简化了原有的 JNI 技术。下面是JNA github地址:https://github.com/java-native-access/jna
1. 简单的一个例子:
/** Simple example of JNA interface mapping and usage. */
public class HelloWorld { public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class); void printf(String format, Object... args); // void printf(const char *format, [argument]);
} public static void main(String[] args) {
CLibrary.INSTANCE.printf("Hello, World\n");
for (int i=0; i < args.length; i++) {
CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
}
}
}
原理是,通过 CLibrary extends Libarary 在其中加载系统的 DLL/so 库,并列出Java要调用的DLL库函数,该函数到DLL库函数直接的映射有JNA来完成。
这样就可以调用DLL/so库中的函数了。
2. 如何使用 JNA 加载多个DLL库,而且它们之间存在依赖关系
http://stackoverflow.com/questions/32609829/load-multiple-libraries-with-jna
Is there a way in JNA to load multiple libraries with Java?
I usually use Native.loadLibrary(...) to load one DLL. But I guess its not working this way because I assign this function call to the instance member.
Let's say I have library foo and library bar. bar has a dependency on foo; it also has a dependency on baz, which we are not mapping with JNA:
public class Foo {
public static final boolean LOADED;
static {
Native.register("foo");
LOADED = true;
}
public static native void call_foo();
}
public class Bar {
static {
// Reference "Foo" so that it is loaded first
if (Foo.LOADED) {
System.loadLibrary("baz");
// Or System.load("/path/to/libbaz.so")
Native.register("bar");
}
}
public static native void call_bar();
}
The call to System.load/loadLibrary will only be necessary if baz is neither on your library load path (PATH/LD_LIBRARY_PATH, for windows/linux respectively) nor in the same directory as bar(windows only).
EDIT
You can also do this via interface mapping:
public interface Foo extends Library {
Foo INSTANCE = (Foo)Native.loadLibrary("foo");
}
public interface Bar extends Library {
// Reference Foo prior to instantiating Bar, just be sure
// to reference the Foo class prior to creating the Bar instance
Foo FOO = Foo.INSTANCE;
Bar INSTANCE = (Bar)Native.loadLibrary("bar");
}
-----------------
3. 自己的代码例子:
public class DDLTest {
public static void main(String[] args){
System.out.println(System.getProperty("java.library.path"));
FingerLibrary.Fingerdll.ZAZCloseDeviceEx(-1);
int r = ID_FprCap.fprCap.LIVESCAN_Init();
ID_FprCap.fprCap.LIVESCAN_Close();
ID_FprCap.fprCap.LIVESCAN_BeginCapture(1);
ID_FprCap.fprCap.LIVESCAN_GetFPRawData(1, "aaaaaa");
r = ID_Fpr.INSTANCE.FP_FeatureMatch("aaaaaaaaaaa", "aaaaaaaaaaa", (float)0.5);
}
public interface FingerLibrary extends Library {
int FPDATASIZE = 256;
int IMGWIDTH = 256;
int IMGHEIGHT = 288;
int IMGSIZE = 73728;
// 窗口消息
int WM_FPMESSAGE = 1024 + 120; // 自定义消息
int FPM_DEVICE = 0x01; // 状态提示
int FPM_PLACE = 0x02; // 请按手指
int FPM_LIFT = 0x03; // 请抬起手指
int FPM_CAPTURE = 0x04; // 采集图像
int FPM_ENROLL = 0x06; // 登记指纹模版
int FPM_GENCHAR = 0x05; // 采集特征点
int FPM_NEWIMAGE = 0x07; // 指纹图像
int RET_OK = 0x1;
int RET_FAIL = 0x0;
FingerLibrary Fingerdll = (FingerLibrary) Native.loadLibrary("ZAZAPIt", FingerLibrary.class);
public int ZAZOpenDeviceEx(int[] hHandle, int nDeviceType, int iCom, int iBaud, int nPackageSize, int iDevNum);
public int ZAZCloseDeviceEx(int handle);
public int ZAZVfyPwd(int pHandle, int nAddr, byte[] pPassword);
public int ZAZReadInfPage(int pHandle, int nAddr, byte[] pInf);
public int ZAZReadIndexTable(int pHandle, int nAddr, int nPage, byte[] UserContent);
// public int FP_FeatureMatch(String pFeatureData1, String pFeatureData2, float pfSimilarity);
}
public interface ID_Fpr extends Library {
ID_Fpr INSTANCE = (ID_Fpr)Native.loadLibrary("ID_Fpr", ID_Fpr.class);
// public int LIVESCAN_Init();
public int FP_FeatureMatch(String pFeatureData1, String pFeatureData2, float pfSimilarity);
//int __stdcall FP_FeatureMatch(unsigned char * pFeatureData1, //输入参数 指纹特征数据1
// unsigned char * pFeatureData2, //输入参数 指纹特征数据2
// float * pfSimilarity); //输出参数 匹配相似度值0.00-1.00
// typedef int (__stdcall *FP_FeatureMatch)(unsigned char * pFeatureData1, //输入参数 指纹特征数据1
// unsigned char * pFeatureData2, //输入参数 指纹特征数据2
// float * pfSimilarity); //输出参数 匹配相似度值0.00-1.00
}
public interface ID_FprCap extends StdCallLibrary {
ID_Fpr fpr = ID_Fpr.INSTANCE ;
ID_FprCap fprCap = (ID_FprCap)Native.loadLibrary("ID_FprCap", ID_FprCap.class);
public int LIVESCAN_Init(); //int __stdcall LIVESCAN_Init();
public int LIVESCAN_Close(); //int __stdcall LIVESCAN_Close();
public int LIVESCAN_BeginCapture(int nChannel);
public int LIVESCAN_GetFPRawData(int nChannel, String pRawData);
// int __stdcall LIVESCAN_BeginCapture( int nChannel );
// int __stdcall LIVESCAN_GetFPRawData(int nChannel,unsigned char *pRawData);
public int LIVESCAN_GetFPBmpData(int nChannel, String pBmpData);
// int __stdcall LIVESCAN_GetFPBmpData(int nChannel, unsigned char *pBmpData);
public int LIVESCAN_EndCapture(int nChannel);
// int __stdcall LIVESCAN_EndCapture(int nChannel );
public int LIVESCAN_IsSupportSetup();
// int __stdcall LIVESCAN_IsSupportSetup();
public int LIVESCAN_GetVersion();
// int __stdcall LIVESCAN_GetVersion();
public int LIVESCAN_GetDesc(String pszDesc);
// int __stdcall LIVESCAN_GetDesc(char pszDesc[1024]);
public int LIVESCAN_GetErrorInfo(int nErrorNo, String pszErrorInfo);
// int __stdcall LIVESCAN_GetErrorInfo(int nErrorNo, char pszErrorInfo[256]);
}
}
JNA 如何 加载多个 存在依赖的 DLL 库的更多相关文章
- java组件不存在解决方案:右侧Maven Projects展开后左上角第一个刷新按钮 刷新后就会从新加载所有java的依赖项了
java组件不存在解决方案:右侧Maven Projects展开后左上角第一个刷新按钮 刷新后就会从新加载所有java的依赖项了 软件:idea 问题产生:其他同事进行开发,引入新java组件后提交 ...
- 重新加载maven项目的依赖项
最近在调试reportNG,测试允许完以后,报告总是使用的testNG的格式,并且只有index和overview两个文件. 找了好多帖子,大家都是那么设置的都没有问题,难道是哥人品不好?错! 大家基 ...
- 安卓---下拉刷新---上拉加载---解决导入library等自生成库文件失败的问题
本文的下拉刷新以及上拉加载都是用PullToRefresh实现的,关于PullToRefresh的介绍以及源码,网上可以找到很多,本人在此不再赘述. PullToRefresh是一套实现非常好的下拉刷 ...
- 关于plsqldev无法正常加载oracle instantclient中的oci.dll的其中一个原因
事情的经过是这样的: 1. 新安装了windows10 系统,装了plsqldev 和 oracle instantclient,以及 instantclient sqlplus. 2.设置好了ORA ...
- Idea开发环境中,开发springboot类型的项目,如果只引入parent节点,不添加依赖节点,maven是不会加载springboot的任何依赖的
在SpringBoot类型的项目中,我本来是要使用pringBoot,创建一个Console项目,我原本在pom.xml中添加paren节点了,天真的认为不需要再添加其他任何依赖了,可是接下来的1个小 ...
- Linux加载/usr/local/lib中的so库
> https://my.oschina.net/u/2306127/blog/1617233 > https://blog.csdn.net/csfreebird/article/det ...
- PHP: php_ldap.dll不能加载解决方案
PHP: php_ldap.dll不能加载解决方案 php.ini中开启 ldap的扩展后,重启服务:phpinfo();中没有ldap apache_error.log 提示:PHP Warning ...
- 带加载进度的Web图片懒加载组件Lazyload
在Web项目中,大量的图片应用会导致页面加载时间过长,浪费不必要的带宽成本,还会影响用户浏览体验. Lazyload 是一个文件大小仅4kb的图片懒加载组件(不依赖其它第三方库),组件会根据用户当前浏 ...
- Spring Boot中采用Mockito来mock所测试的类的依赖(避免加载spring bean,避免启动服务器)
最近试用了一下Mockito,感觉真的挺方便的.举几个应用实例: 1,需要测试的service中注入的有一个dao,而我并不需要去测试这个dao的逻辑,只需要对service进行测试.这个时候怎么办呢 ...
随机推荐
- mysql int(3)与int(11)的区别
总结,int(M) zerofill,加上zerofill后M才表现出有点点效果,比如 int(3) zerofill,你插入到数据库里的是10,则实际插入为010,也就是在前面补充加了一个0.如果i ...
- Pydev Debugger not working with breakpoints
I have a simple test module: print("fish")print("sticks")It runs pretty fast as ...
- springmvc restful配置有一个小小的坑坑
首先web.xml配置 <!-- spring-mvc --> <servlet> <servlet-name>springServlet</servlet- ...
- [javaSE] 反射-动态加载类
Class.forName(“类的全称”) ①不仅表示了类的类类型,还代表了动态加载类 ②请大家区分编译,运行 ③编译时刻加载类是静态加载类,运行时刻加载类是动态加载类 Ⅰ所有的new对象都是静态加载 ...
- PHP中使用redis执行lua脚本示例
摸索了一下在PHP中如何使用redis执行lua脚本,写了一个脚本如下,供以后参考 <?php $redis = new Redis(); #实例化redis类 $redis->conne ...
- windows根据端口号找进程
d:\>netstat -ano | findstr "7777" TCP 127.0.0.1:7776 127.0.0.1:7777 ESTABLISHED 11764 T ...
- DDD为何叫好不叫座?兼论DCI与业务分析的方法论
今天,仔细阅读了园子里面的一个朋友写的<一缕阳光:DDD(领域驱动设计)应对具体业务场景,如何聚焦 Domain Model(领域模型)?>(http://www.cnblogs.com/ ...
- C#操作Excel的函数
对于Excel的数据处理功能,大家都已经了解. 我们经常需要将数据导入到Excel,或直接打开Excel文档,读写文件操作,这需要用到ExcelHelper类,有了这个类,这些操作大大的减少我们工作量 ...
- servletcontext监听器的启动位置以及tomcat和eclipse的目录结构
情景: 想在应用启动的时候就加载spring容器 在ServletContextListener.contextInitialized()中加载spring容器 ApplicationContext ...
- this指向问题
我今天下午本来想做个就是tr鼠标移出之后过三秒把对应的input添加hiddens类 然后我就这样写了 $('.table>tbody>tr').mouseout(function(){ ...