Search code examples
javajna

Specified module could not being found loading DLL in JNA


I have this class initialiser:

System.setProperty("java.library.path", "C:\\Users\\lucas\\Desktop\\libraries");
System.loadLibrary("libTTARCHHelper");
TTARCH_LIBRARY=(TtarchLibrary)Native.loadLibrary(TtarchLibrary.class);

And the DLL is located at C:\\Users\\lucas\\Desktop\\libraries\\libTTARCHHelper.dll

If I run this in Eclipse I get 'The specified module could not be found' and if I run it as a JAR I get 'no libTTARCHHelper in java.library.path'.

How do I fix these? I even tried putting the DLL in a folder in the PATH environment variable.

Full debug using the file direct load:

Aug 26, 2020 12:28:51 AM com.sun.jna.Native extractFromResourcePath
INFO: Looking in classpath from jdk.internal.loader.ClassLoaders$AppClassLoader@c387f44 for /com/sun/jna/win32-x86-64/jnidispatch.dll
Aug 26, 2020 12:28:51 AM com.sun.jna.Native extractFromResourcePath
INFO: Found library resource at jar:file:/C:/Users/lucas/Desktop/My%20Stuff/Eclipse%20Workspaces/Build%20Paths/jna-5.6.0.jar!/com/sun/jna/win32-x86-64/jnidispatch.dll
Aug 26, 2020 12:28:51 AM com.sun.jna.Native extractFromResourcePath
INFO: Extracting library to C:\Users\lucas\AppData\Local\Temp\jna-103324076\jna5931694657592960137.dll
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Looking for library 'C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll'
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Adding paths from jna.library.path: null
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Trying C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Loading failed with message: The specified module could not be found.

Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Adding system paths: []
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Trying C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Loading failed with message: The specified module could not be found.

Aug 26, 2020 12:28:51 AM com.sun.jna.Native extractFromResourcePath
INFO: Looking in classpath from jdk.internal.loader.ClassLoaders$AppClassLoader@c387f44 for C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Loading failed with message: Native library (win32-x86-64/C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll) not found in resource path (C:\Users\lucas\eclipse-workspace\TEST\bin;C:\Users\lucas\Desktop\My Stuff\Eclipse Workspaces\Build Paths\jna-5.6.0.jar)
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll':
The specified module could not be found.

The specified module could not be found.

The rest of the trace:

Native library (win32-x86-64/C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll) not found in resource path (C:\Users\lucas\eclipse-workspace\TEST\bin;C:\Users\lucas\Desktop\My Stuff\Eclipse Workspaces\Build Paths\jna-5.6.0.jar)
    at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:301)
    at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:461)
    at com.sun.jna.Library$Handler.<init>(Library.java:192)
    at com.sun.jna.Native.loadLibrary(Native.java:646)
    at com.sun.jna.Native.loadLibrary(Native.java:630)
    at com.test.TTARCHHelper.<clinit>(TTARCHHelper.java:17)
    at com.test.main.main(main.java:6)
    Suppressed: java.lang.UnsatisfiedLinkError: The specified module could not be found.

        at com.sun.jna.Native.open(Native Method)
        at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:191)
        ... 6 more
    Suppressed: java.lang.UnsatisfiedLinkError: The specified module could not be found.

        at com.sun.jna.Native.open(Native Method)
        at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:204)
        ... 6 more
    Suppressed: java.io.IOException: Native library (win32-x86-64/C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll) not found in resource path (C:\Users\lucas\eclipse-workspace\TEST\bin;C:\Users\lucas\Desktop\My Stuff\Eclipse Workspaces\Build Paths\jna-5.6.0.jar)
        at com.sun.jna.Native.extractFromResourcePath(Native.java:1095)
        at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:275)
        ... 6 more

So it looks like it doesnt find it , It throws a

java.lang.UnsatisfiedLinkError: The specified module could not be found.

I found the problem, the DLLs were for the wrong bit. Eclipse runs on 64 and the JAR uses the 32bit JVM. I downloaded the 32 bit dlls too and now it works when I run the JAR. However the 64 bit dlls in Eclipse keep throwing the module exception above


Solution

  • JNA will look for libraries at locations in the jna.library.path environment variable.

    One option is to specify it as a command line option in Eclipse's run configuration. In the Run menu, select Run Configuration. Go to the (x)=Arguments tab. Add this in the VM arguments field:

    -Djna.library.path=C:\Users\lucas\Desktop\libraries

    You can also programmatically set it in your code before attempting to load the DLL:

    System.setProperty("jna.library.path", "C:\\Users\\lucas\\Desktop\\libraries");

    As you later indicated with the debug log, it may be finding the DLL but failing to open it. Possible reasons for this can be:

    • The DLL is runtime-dependent on another DLL on your system that's not in the appropriate path(s). A common one is the Visual C++ Distributable. You may need to consult the DLL documentation for more information. You can sometimes use Process Monitor from the SysInternals library to try to see what it's opening.
    • Having a duplicate/incompatible version of a dependent DLL as above
    • Running a 64-bit JVM and trying to open a DLL that's not 64-bit compatible, or vice-versa
    • Your Java program may not have permission to read the directory where your library is located

    To help diagnose the problem, look at the full stack trace. While the most recent error message may not be relevant (file not found in a "backup" search) if you look down the trace you may see multiple suppressed exceptions from earlier attempts to open a DLL which may give you more informative debugging information.