You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JNI调用C++生成的DLL时出现java.lang.UnsatisfiedLinkError: 无法找到依赖库的问题排查求助

JNI调用C++生成的DLL时出现java.lang.UnsatisfiedLinkError: 无法找到依赖库的问题排查求助

各位好,我之前有一套通过JNI实现Java调用C的示例代码,好几年前能正常跑起来。现在项目需要用到Java调用C的功能,我针对当前的编译器版本更新了这个示例,结果一直卡在同一个错误上,实在没辙了,想请大家帮忙看看。

先说说我的环境:

  • Java:64位Temurin-17.0.12+7(平时和FRC/WPILib打交道用的这个版本)
  • C编译:64位g(GNU C++17 (GCC) 15.1.0 x86_64-w64-mingw32),主要是需要用它的__builtin_popcount功能

我本身对JNI的路径规则也有了解:比如更新PATH后要重启终端/VS Code才会生效;java.library.path如果是用户手动指定的话,得把所有依赖路径都列全(比如要包含当前目录的话得写成java.library.path="%PATH%;.");VS Code这类IDE会自动处理一些细节,比如把DLL复制到class文件目录,.classpath文件也会用来指定文件位置这些。

但现在不管我怎么试,运行时都会报:

java.lang.UnsatisfiedLinkError: C:\Users\bike1\JNItest\HelloCpp.dll: Can't find dependent libraries

我已经尝试了各种方法:调整DLL的存放路径、修改java.library.path这类JVM参数、调整编译时的包含路径和搜索路径,甚至照搬网上的各种JNI示例代码,但全都报同样的错误。最头疼的是,我找不到任何额外的调试信息来告诉我到底缺失了哪个依赖库,完全摸不着头绪。

下面是我用的编译脚本compile.bat:

@echo on
echo compile java class
set JAVA_HOME=C:\Users\Public\wpilib\2025\jdk
javac -version
type %JAVA_HOME%\release
javac HelloJNI.java
javap -p -s HelloJNI
echo generate header file
javac -h . HelloJNI.java
echo compile DLL
g++ -v -m64 -Wl,--add-stdcall-alias -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o HelloCpp.dll main.cpp
echo run java program
java -Djava.library.path=. HelloJNI
rem java HelloJNI
echo Clean up
dir
del *.class; del *.dll, del *.h
pause

以及编译运行的输出片段:

C:\Users\bike1\JNItest>compile
C:\Users\bike1\JNItest>echo compile java class
compile java class
C:\Users\bike1\JNItest>set JAVA_HOME=C:\Users\Public\wpilib\2025\jdk
C:\Users\bike1\JNItest>javac -version
javac 17.0.12
C:\Users\bike1\JNItest>type C:\Users\Public\wpilib\2025\jdk\release
IMPLEMENTOR="Eclipse Adoptium"
IMPLEMENTOR_VERSION="Temurin-17.0.12+7"
...
C:\Users\bike1\JNItest>javac HelloJNI.java
C:\Users\bike1\JNItest>javap -p -s HelloJNI
Compiled from "HelloJNI.java"
...
C:\Users\bike1\JNItest>echo generate header file
generate header file
C:\Users\bike1\JNItest>javac -h . HelloJNI.java
C:\Users\bike1\JNItest>echo compile DLL
compile DLL
C:\Users\bike1\JNItest>g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=C:/Program\ Files/mingw64/bin/../x86_64-w64-mingw32/lib/gcc/x86_64-w64-mingw32/15.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
...
C:\Users\bike1\JNItest>g++ -v -m64 -Wl,--add-stdcall-alias -I"C:\Users\Public\wpilib\2025\jdk\include" -I"C:\Users\Public\wpilib\2025\jdk\include\win32" -shared -o HelloCpp.dll main.cpp
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=C:/Program\ Files/mingw64/bin/../x86_64-w64-mingw32/lib/gcc/x86_64-w64-mingw32/15.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-src/configure --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --disable-bootstrap --enable-targets=all --enable-languages=c,c++,lto --enable-seh-exceptions --with-arch=core2 --with-tune=generic --enable-threads=posix --disable-nls --enable-shared=libstdc++ --enable-static --enable-libatomic --enable-fully-dynamic-string --enable-lto --enable-plugins --enable-libgomp --with-dwarf2 --enable-mingw-wildcard=platform --disable-win32-registry --enable-version-specific-runtime-libs --enable-checking=release --enable-default-compressed-debug-sections-algorithm=zstd --disable-cloog-version-check --enable-cloog-backend=isl --with-gmp=/home/vm/mingw/host/x86_64-w64-mingw32 --with-mpfr=/home/vm/mingw/host/x86_64-w64-mingw32 --with-mpc=/home/vm/mingw/host/x86_64-w64-mingw32 --with-isl=/home/vm/mingw/host/x86_64-w64-mingw32 --with-cloog=...

请问大家有没有什么方法能帮我定位到底缺失了哪个依赖库?或者我的编译、运行流程里有什么遗漏的地方?真的非常感谢!

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 07:08:12