JNA:本机库依赖项和 JAR 提取
我将通过 JNA 访问 MSP430.dll(参见此处).但是这个库依赖于与 MSP430.dll 捆绑在一起的其他本机库.这个依赖本机库称为 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 的模块错误.
I will access the MSP430.dll (see here) via JNA. But this library has a dependency to other native library, which cames bundled with the MSP430.dll. This dependend native library is called HIL.dll. With use of jna.library.path all does work perfect. Now I would bundle the native librarys to my own .jar package. Then I would use the automatic native library loading from jar of the JNA framework. But this makes problems with native librarys that have dependencies. If I package the MSP430.dll and HIL.dll to my .jar package then I get following error: "JNA java.lang.UnsatisfiedLinkError and The specified module could not be found". This means, it was not possible to resolve the dependencies of the MSP430.dll to the HIL.dll. In the debug outputs of JNA i can see, only the MSP430.dll will extract from .jar package. In this context the error is understandable. In the next trail, I load the HIL.dll explicit and I can see the extraction of HIL.dll then of the MSP430.dll. But I get still the module error from JNA.
我查看了提取方法的 JNA 代码.我看到 JNA 将本机库提取到系统临时目录并在其中创建自己的临时目录.但现在我认为问题是,JNA 为本地库创建临时文件,带有jna"前缀和唯一生成的数值.这里是 JNA 的输出:
I have looked to the JNA code of the extraction method. I see JNA extracts the native librarys to system temp directory and creates an own temp directory in their. But now I think the problem is, JNA creats temp files for the native librarys wit "jna" prefix and a unique generated numeric value. Here the output of JNA:
Found library 'HIL.dll' at C:UsersRD3AppDataLocalTempjna-80961jna1305152974718331988.dll
我认为 MSP430.dll 需要未重命名的 HIL.dll 才能找到它.我有直接加载 HIL.dll 的 testet,并在应用程序中进行第一次调用
I think the MSP430.dll needs the HIL.dll unrenamed for finding it. I have testet with direct loading of the HIL.dll with following as first call in the application
System.loadLibrary(C:\absolutepath\HIL.dll);
然后通过 JNA 从 .jar 包中仅加载 MSP430.dll,这没有问题.之后,我将 HIL.dll 重命名为 lol.dll 并使用此调用:
Then loads only MSP430.dll via JNA from .jar package, this works without problems. After that I rename the HIL.dll to lol.dll and use this call:
System.loadLibrary(C:\absolutepath\lol.dll);
然后我再次从 JNA 收到模块错误.MSP430.dll 无法解析重命名的 HIL.dll.
then I gets the module error again from JNA. The MSP430.dll was not able to resolve the renamed HIL.dll.
是否可以将具有依赖关系的本机库打包到 .jar 包并使用 JNA 加载?
It is anyway possible to package a native library with dependencies to .jar package and loading with JNA?
这里是通过 JNA 显式加载 HIL.dll 的 JNA 调试输出:
Here the JNA debug output with explicit loading of HIL.dll via JNA:
C:UsersRD3Desktop>call "C:Program Files (x86)Javajre1.8.0_25injava.exe"
-Djna.debug_load=true -jar C:DataWorkspaceNetBeansMspApiTest argetMspApiTest-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:UsersRD3AppDataLocalTempjna-80961jna1305152974718331988.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:UsersRD3Desktop>
最好的问候
推荐答案
假设您遇到的唯一问题是依赖库的名称,请参阅 Native.extractFromResourcePath()
.您可以使用它来提取非显式依赖项并使用 File.rename()
来确保库具有您想要的名称.
Assuming that the only issue you're having is the name of the dependent library, see Native.extractFromResourcePath()
. You can use that to extract your non-explicit dependencies and use File.rename()
to ensure the library has the name you want.
相关文章