Can't find dependent libraires when call System.loadLibrary on Windows 64bit

38 Views Asked by At

I got Can't find dependent libraries message when I call System.loadLibrary("someDll").

I have the dll file in the correct location so I got any clue why UnsatisfiedLinkError occurs.

Here is my code(pretty simple)

// NativeCaller.java
public class NativeCaller {

    static {
        try {
            System.loadLibrary("SomeDll");
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
    public native void hello();
}

// Application.java
public class Application {

    private void loadJni() {
        NativeCaller wrapper = new NativeCaller();
        wrapper.hello();
    }

    public static void main(String[] args) throws Exception {
        Application application = new Application();
        application.loadJni();
        System.out.println("done");
    }
}

and here is the message printed

"C:\Program Files\OpenLogic\jdk-8.0.372.07-hotspot\bin\java.exe" -Djava.library.path=C:\Users\jyoh\IdeaProjects\jnitest\src\native;C:\Users\jyoh\lib; "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.2\lib\idea_rt.jar=2376:C:\Program Files\JetBrains\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath 
... org.owls.Application
Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Users\jyoh\lib\SomeDll.dll: Can't find dependent libraries
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1934)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1850)
    at java.lang.Runtime.loadLibrary0(Runtime.java:843)
    at java.lang.System.loadLibrary(System.java:1136)
    at org.owls.jni.NativeCaller.<clinit>(NativeCaller.java:8)
    at org.owls.Application.loadJni(Application.java:8)
    at org.owls.Application.main(Application.java:14)

Process finished with exit code 1

but I can see my file on terminal like below

> PS C:\Users\jyoh> ls -h C:\Users\jyoh\lib\SomeDll.dll


    Directory: C:\Users\jyoh\lib


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----      2023-08-24   PM 3:30        1235456 SomeDll.dll


got any clues?

I have tested this on Windows11 64 bit and my g++ info like below

PS C:\Users\jyoh> g++ -v
Using built-in specs.
COLLECT_GCC=C:\Program Files\mingw64\bin\g++.exe
COLLECT_LTO_WRAPPER=C:/Program\ Files/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/8.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-8.1.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-seh-rev0, Built by MinGW-W64 project' --with-bugurl=https://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS=' -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' LDFLAGS='-pipe -fno-ident -L/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/lib -L/c/mingw810/prerequisites/x86_64-zlib-static/lib -L/c/mingw810/prerequisites/x86_64-w64-mingw32-static/lib '
Thread model: posix
gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
0

There are 0 best solutions below