Reputation: 252
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.
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:\Users\RD3\AppData\Local\Temp\jna-80961\jna1305152974718331988.dll
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);
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);
then I gets the module error again from JNA. The MSP430.dll was not able to resolve the renamed HIL.dll.
It is anyway possible to package a native library with dependencies to .jar package and loading with JNA?
Here the JNA debug output with explicit loading of HIL.dll via JNA:
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>
Best regards
Upvotes: 4
Views: 4555
Reputation: 10069
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.
Upvotes: 3