JNA:本机库依赖项和JAR提取

时间:2015-01-07 22:26:12

标签: java dll jar dependencies jna

我将通过JNA访问MSP430.dll(请参阅此处)。但是这个库依赖于其他本地库,它与MSP430.dll捆绑在一起。此dependend本机库称为HIL.dll。使用jna.library.path一切都很完美。现在我将原生图书馆捆绑到我自己的.jar包中。然后我将使用从JNA框架的jar加载自动本机库。但这会使具有依赖性的本机库出现问题。如果我将MSP430.dll和HIL.dll打包到我的.jar包中,则会出现以下错误:“JNA java.lang.UnsatisfiedLinkError并且找不到指定的模块”。这意味着,无法解决MSP430.dll与HIL.dll的依赖关系。在JNA的调试输出中,我可以看到,只有MSP430.dll将从.jar包中提取。在这种情况下,错误是可以理解的。在下一个跟踪中,我显式加载HIL.dll,然后我可以看到HIL.dll的提取然后是MSP430.dll。但我仍然从JNA得到模块错误。

我已经查看了提取方法的JNA代码。我看到JNA将本机库提取到系统临时目录并在其中创建自己的临时目录。但现在我认为问题是,JNA为本机库创建了具有“jna”前缀和唯一生成的数值的临时文件。这里是JNA的输出:

Found library 'HIL.dll' at C:\Users\RD3\AppData\Local\Temp\jna-80961\jna1305152974718331988.dll

我认为MSP430.dll需要H16.dll才能找到它。我已经试着直接加载HIL.dll,并在应用程序中首次调用

System.loadLibrary(C:\\absolutepath\\HIL.dll);

然后从.jar包通过JNA加载MSP430.dll,这没有问题。之后,我将HIL.dll重命名为lol.dll并使用此调用:

System.loadLibrary(C:\\absolutepath\\lol.dll);

然后我再次从JNA获取模块错误。 MSP430.dll无法解析重命名的HIL.dll。

无论如何,可以将具有依赖项的本机库打包到.jar包并使用JNA加载?

此处JNA调试输出通过JNA显式加载HIL.dll:

C:\Users\RD3\Desktop>call "C:\Program Files (x86)\Java\jre1.8.0_25\bin\java.exe"
 -Djna.debug_load=true -jar C:\Data\Workspace\NetBeans\MspApiTest\target\MspApiTest-1.0.0-SNAPSHOT-jar-with-dependencies.jar
Looking in classpath from sun.misc.Launcher$AppClassLoader@1f96302 for /com/sun/jna/win32-x86/jnidispatch.dll
Found library resource at jar:file:/C:/Data/mstandfuss/Workspace/NetBeans/MspApiTest/target/MspApiTest-1.0.0-SNAPSHOT-jar-with-dependencies.jar!/com/sun/jna/win32-x86/jnidispatch.dll
Looking for library 'HIL.dll'
Adding paths from jna.library.path: null
Trying HIL.dll
Adding system paths: []
Trying HIL.dll
Looking for lib- prefix
Trying libHIL.dll
Looking in classpath from sun.misc.Launcher$AppClassLoader@1f96302 for HIL.dll
Found library resource at jar:file:/C:/Data/mstandfuss/Workspace/NetBeans/MspApiTest/target/MspApiTest-1.0.0-SNAPSHOT-jar-with-dependencies.jar!/win32-x86/HIL.dll
Found library 'HIL.dll' at C:\Users\RD3\AppData\Local\Temp\jna-80961\jna1305152974718331988.dll
Looking for library 'MSP430.dll'
Adding paths from jna.library.path: null
Trying MSP430.dll
Adding system paths: []
Trying MSP430.dll
Looking for lib- prefix
Trying libMSP430.dll
Looking in classpath from sun.misc.Launcher$AppClassLoader@1f96302 for MSP430.dll
Found library resource at jar:file:/C:/Data/mstandfuss/Workspace/NetBeans/MspApi
Test/target/MspApiTest-1.0.0-SNAPSHOT-jar-with-dependencies.jar!/win32-x86/MSP430.dll
Exception in thread "main" java.lang.UnsatisfiedLinkError: Das angegebene Modul wurde nicht gefunden.

        at com.sun.jna.Native.open(Native Method)
        at com.sun.jna.Native.open(Native.java:1759)
        at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:260)
        at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:398)
        at com.sun.jna.Library$Handler.<init>(Library.java:147)
        at com.sun.jna.Native.loadLibrary(Native.java:412)
        at com.sun.jna.Native.loadLibrary(Native.java:391)
        at de.sitec.jmspflash.Msp430Native.<clinit>(Msp430Native.java:22)
        at de.sitec.jmspflash.Msp430Impl.init(Msp430Impl.java:50)
        at de.sitec.jmspflash.Msp430Impl.createMsp430Impl(Msp430Impl.java:36)
        at de.sitec.mspapitest.App.main(App.java:34)

C:\Users\RD3\Desktop>

祝你好运

1 个答案:

答案 0 :(得分:2)

假设您遇到的唯一问题是从属库的名称,请参阅Native.extractFromResourcePath()。您可以使用它来提取非显式依赖项,并使用File.rename()确保库具有您想要的名称。