JNA 的出现,极大的简化了原有的 JNI 技术。下面是JNA github地址:https://github.com/java-native-access/jna

1. 简单的一个例子

  1. /** Simple example of JNA interface mapping and usage. */
  2. public class HelloWorld {
  3.  
  4. public interface CLibrary extends Library {
  5. CLibrary INSTANCE = (CLibrary)Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);
  6.  
  7. void printf(String format, Object... args); // void printf(const char *format, [argument]);
  8. }
  9.  
  10. public static void main(String[] args) {
  11. CLibrary.INSTANCE.printf("Hello, World\n");
  12. for (int i=0; i < args.length; i++) {
  13. CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
  14. }
  15. }
  16. }

原理是,通过 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 barbar has a dependency on foo; it also has a dependency on baz, which we are not mapping with JNA:

  1. public class Foo {
  2. public static final boolean LOADED;
  3. static {
  4. Native.register("foo");
  5. LOADED = true;
  6. }
  7. public static native void call_foo();
  8. }
  9. public class Bar {
  10. static {
  11. // Reference "Foo" so that it is loaded first
  12. if (Foo.LOADED) {
  13. System.loadLibrary("baz");
  14. // Or System.load("/path/to/libbaz.so")
  15. Native.register("bar");
  16. }
  17. }
  18. public static native void call_bar();
  19. }

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:

  1. public interface Foo extends Library {
  2. Foo INSTANCE = (Foo)Native.loadLibrary("foo");
  3. }
  4. public interface Bar extends Library {
  5. // Reference Foo prior to instantiating Bar, just be sure
  6. // to reference the Foo class prior to creating the Bar instance
  7. Foo FOO = Foo.INSTANCE;
  8. Bar INSTANCE = (Bar)Native.loadLibrary("bar");
  9. }

-----------------

3. 自己的代码例子:

  1. public class DDLTest {
  2.  
  3. public static void main(String[] args){
  4. System.out.println(System.getProperty("java.library.path"));
  5. FingerLibrary.Fingerdll.ZAZCloseDeviceEx(-1);
  6.  
  7. int r = ID_FprCap.fprCap.LIVESCAN_Init();
  8. ID_FprCap.fprCap.LIVESCAN_Close();
  9. ID_FprCap.fprCap.LIVESCAN_BeginCapture(1);
  10. ID_FprCap.fprCap.LIVESCAN_GetFPRawData(1, "aaaaaa");
  11. r = ID_Fpr.INSTANCE.FP_FeatureMatch("aaaaaaaaaaa", "aaaaaaaaaaa", (float)0.5);
  12. }
  13.  
  14. public interface FingerLibrary extends Library {
  15. int FPDATASIZE = 256;
  16. int IMGWIDTH = 256;
  17. int IMGHEIGHT = 288;
  18. int IMGSIZE = 73728;
  19.  
  20. // 窗口消息
  21. int WM_FPMESSAGE = 1024 + 120; // 自定义消息
  22. int FPM_DEVICE = 0x01; // 状态提示
  23. int FPM_PLACE = 0x02; // 请按手指
  24. int FPM_LIFT = 0x03; // 请抬起手指
  25. int FPM_CAPTURE = 0x04; // 采集图像
  26. int FPM_ENROLL = 0x06; // 登记指纹模版
  27. int FPM_GENCHAR = 0x05; // 采集特征点
  28. int FPM_NEWIMAGE = 0x07; // 指纹图像
  29. int RET_OK = 0x1;
  30. int RET_FAIL = 0x0;
  31.  
  32. FingerLibrary Fingerdll = (FingerLibrary) Native.loadLibrary("ZAZAPIt", FingerLibrary.class);
  33.  
  34. public int ZAZOpenDeviceEx(int[] hHandle, int nDeviceType, int iCom, int iBaud, int nPackageSize, int iDevNum);
  35. public int ZAZCloseDeviceEx(int handle);
  36. public int ZAZVfyPwd(int pHandle, int nAddr, byte[] pPassword);
  37. public int ZAZReadInfPage(int pHandle, int nAddr, byte[] pInf);
  38. public int ZAZReadIndexTable(int pHandle, int nAddr, int nPage, byte[] UserContent);
  39.  
  40. // public int FP_FeatureMatch(String pFeatureData1, String pFeatureData2, float pfSimilarity);
  41. }
  42.  
  43. public interface ID_Fpr extends Library {
  44. ID_Fpr INSTANCE = (ID_Fpr)Native.loadLibrary("ID_Fpr", ID_Fpr.class);
  45. // public int LIVESCAN_Init();
  46.  
  47. public int FP_FeatureMatch(String pFeatureData1, String pFeatureData2, float pfSimilarity);
  48. //int __stdcall FP_FeatureMatch(unsigned char * pFeatureData1, //输入参数 指纹特征数据1
  49. // unsigned char * pFeatureData2, //输入参数 指纹特征数据2
  50. // float * pfSimilarity); //输出参数 匹配相似度值0.00-1.00
  51.  
  52. // typedef int (__stdcall *FP_FeatureMatch)(unsigned char * pFeatureData1, //输入参数 指纹特征数据1
  53. // unsigned char * pFeatureData2, //输入参数 指纹特征数据2
  54. // float * pfSimilarity); //输出参数 匹配相似度值0.00-1.00
  55. }
  56.  
  57. public interface ID_FprCap extends StdCallLibrary {
  58. ID_Fpr fpr = ID_Fpr.INSTANCE ;
  59. ID_FprCap fprCap = (ID_FprCap)Native.loadLibrary("ID_FprCap", ID_FprCap.class);
  60.  
  61. public int LIVESCAN_Init(); //int __stdcall LIVESCAN_Init();
  62. public int LIVESCAN_Close(); //int __stdcall LIVESCAN_Close();
  63. public int LIVESCAN_BeginCapture(int nChannel);
  64. public int LIVESCAN_GetFPRawData(int nChannel, String pRawData);
  65. // int __stdcall LIVESCAN_BeginCapture( int nChannel );
  66. // int __stdcall LIVESCAN_GetFPRawData(int nChannel,unsigned char *pRawData);
  67.  
  68. public int LIVESCAN_GetFPBmpData(int nChannel, String pBmpData);
  69. // int __stdcall LIVESCAN_GetFPBmpData(int nChannel, unsigned char *pBmpData);
  70.  
  71. public int LIVESCAN_EndCapture(int nChannel);
  72. // int __stdcall LIVESCAN_EndCapture(int nChannel );
  73.  
  74. public int LIVESCAN_IsSupportSetup();
  75. // int __stdcall LIVESCAN_IsSupportSetup();
  76.  
  77. public int LIVESCAN_GetVersion();
  78. // int __stdcall LIVESCAN_GetVersion();
  79.  
  80. public int LIVESCAN_GetDesc(String pszDesc);
  81. // int __stdcall LIVESCAN_GetDesc(char pszDesc[1024]);
  82.  
  83. public int LIVESCAN_GetErrorInfo(int nErrorNo, String pszErrorInfo);
  84. // int __stdcall LIVESCAN_GetErrorInfo(int nErrorNo, char pszErrorInfo[256]);
  85. }
  86. }

  

JNA 如何 加载多个 存在依赖的 DLL 库的更多相关文章

  1. java组件不存在解决方案:右侧Maven Projects展开后左上角第一个刷新按钮 刷新后就会从新加载所有java的依赖项了

    java组件不存在解决方案:右侧Maven Projects展开后左上角第一个刷新按钮 刷新后就会从新加载所有java的依赖项了 软件:idea 问题产生:其他同事进行开发,引入新java组件后提交 ...

  2. 重新加载maven项目的依赖项

    最近在调试reportNG,测试允许完以后,报告总是使用的testNG的格式,并且只有index和overview两个文件. 找了好多帖子,大家都是那么设置的都没有问题,难道是哥人品不好?错! 大家基 ...

  3. 安卓---下拉刷新---上拉加载---解决导入library等自生成库文件失败的问题

    本文的下拉刷新以及上拉加载都是用PullToRefresh实现的,关于PullToRefresh的介绍以及源码,网上可以找到很多,本人在此不再赘述. PullToRefresh是一套实现非常好的下拉刷 ...

  4. 关于plsqldev无法正常加载oracle instantclient中的oci.dll的其中一个原因

    事情的经过是这样的: 1. 新安装了windows10 系统,装了plsqldev 和 oracle instantclient,以及 instantclient sqlplus. 2.设置好了ORA ...

  5. Idea开发环境中,开发springboot类型的项目,如果只引入parent节点,不添加依赖节点,maven是不会加载springboot的任何依赖的

    在SpringBoot类型的项目中,我本来是要使用pringBoot,创建一个Console项目,我原本在pom.xml中添加paren节点了,天真的认为不需要再添加其他任何依赖了,可是接下来的1个小 ...

  6. Linux加载/usr/local/lib中的so库

    > https://my.oschina.net/u/2306127/blog/1617233 > https://blog.csdn.net/csfreebird/article/det ...

  7. PHP: php_ldap.dll不能加载解决方案

    PHP: php_ldap.dll不能加载解决方案 php.ini中开启 ldap的扩展后,重启服务:phpinfo();中没有ldap apache_error.log 提示:PHP Warning ...

  8. 带加载进度的Web图片懒加载组件Lazyload

    在Web项目中,大量的图片应用会导致页面加载时间过长,浪费不必要的带宽成本,还会影响用户浏览体验. Lazyload 是一个文件大小仅4kb的图片懒加载组件(不依赖其它第三方库),组件会根据用户当前浏 ...

  9. Spring Boot中采用Mockito来mock所测试的类的依赖(避免加载spring bean,避免启动服务器)

    最近试用了一下Mockito,感觉真的挺方便的.举几个应用实例: 1,需要测试的service中注入的有一个dao,而我并不需要去测试这个dao的逻辑,只需要对service进行测试.这个时候怎么办呢 ...

随机推荐

  1. JAVA实现的微信扫描二维码支付

    吐槽一下 支付项目采用springMvc+Dubbo架构实现,只对外提供接口. 话说,为什么微信支付比支付宝来的晚了那么一点,一句话,那一阵挺忙的,然后就没有时间整理,最近做完支付宝支付,顺便也把微信 ...

  2. 2016 年青岛网络赛---Sort(k叉哈夫曼)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5884 Problem Description Recently, Bob has just learn ...

  3. 容器--HashMap

    一.前言 看了下上一篇博客已经是半个月前,将近20天前了,很惭愧没有坚持下来,这期间主要是受奥运会和王宝强事件的影响,另外加上HashMap中关于rehash的实现比较不好理解,所以就一拖再拖.如果能 ...

  4. android实现两个activity数据交互

    android如何实现两个Activity数据交互?主要是根据Intent的携带功能,intent可以携带很多信息,比如Bundle,URI甚至对象(此时要序列化,并且对象里面的成员变量如果是对象,也 ...

  5. VS2010添加类失败问题,弹出错误框,提示 CodeModel操作失败,无法访问标记数据库

    我在使用VS2010添加类的时候,会弹出一个错误框,提示 CodeModel操作失败,可以无法访问标记数据库 英文版是 CodeModel operation failed,Possibly cann ...

  6. 拖放 js

    之前被小伙伴问自己能不能写一个简单的原生的 我稍微犹豫了下  这次重新学习下拖拽的过程  分享下  参考 JavaScript高级程序设计 必要的准备 自定义事件(实现事件模型)  简单来说事件模型就 ...

  7. xwamp 目录结构设计

    xwamp 目录结构设计原文来自:http://www.xwamp.com/make/directory-structure. 目录说明 所有程序都统一放在D盘下的xwamp目录. 目录列表 Apac ...

  8. 杭电acm2029-Palindromes _easy version

    Problem Description “回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串.请写一个程序判断读入的字符串是否是“回文”.   Input 输入包 ...

  9. Electron笔记

    一个能让你用Web技术开发桌面应用的开源项目.这里做一个笔记(非正式文章): 官网地址:http://electron.atom.io/ API相关 Electron提供的主进程接口.渲染进程接口.共 ...

  10. chrome developer tool—— 断点调试篇

    断点,调试器的功能之一,可以让程序中断在需要的地方,从而方便其分析.也可以在一次调试中设置断点,下一次只需让程序自动运行到设置断点位置,便可在上次设置断点的位置中断下来,极大的方便了操作,同时节省了时 ...