JNI与共享库开发问题:生成DLL后Java程序报UnsatisfiedLinkError
Hey there, let's dig into that UnsatisfiedLinkError you're hitting with your sritest()V method. This is super common when working with JNI, so let's break down the most likely fixes step by step:
Common Causes & Fixes for
UnsatisfiedLinkError: sritest ()V 1. Double-Check JNI Method Naming & Signature
JNI enforces strict naming rules for native methods, and even tiny mismatches will trigger this error:
- Your C++ implementation must exactly match the method signature generated by
javah(orjavac -hfor JDK 10+). For avoid sritest()method in a top-levelMainclass, the generated header should look like:JNIEXPORT void JNICALL Java_Main_sritest(JNIEnv *, jobject); - Make sure your C++ code uses this exact signature—don't forget the mandatory
JNIEnv*andjobjectparameters, and keep the method name case-sensitive (sritestmust match Java's method name perfectly).
2. Verify DLL Loading & Library Path
- In your Java code, confirm you're loading the DLL correctly:
static { // Use the DLL name without ".dll" suffix System.loadLibrary("your-library-name"); // Or use the full path if you're not relying on java.library.path // System.load("C:\\path\\to\\your-library.dll"); } - Check if your DLL is in Java's library path by printing:
If your DLL's directory isn't listed, either move the DLL to one of the listed directories, or launch your Java program with:System.out.println(System.getProperty("java.library.path"));java -Djava.library.path="C:\\path\\to\\dll-directory" Main
3. Fix MinGW Compilation Flags
Compiling JNI code requires linking against JDK headers and using the right flags:
- Include the JDK's
includeandinclude/win32directories in your compile command:g++ -I"C:\\Program Files\\Java\\jdk<your-version>\\include" -I"C:\\Program Files\\Java\\jdk<your-version>\\include\\win32" -shared -o your-library.dll your-code.cpp - Critical notes:
- Use the
-sharedflag to generate a DLL (not a regular executable). - Match bitness: If your JDK is 64-bit, use 64-bit MinGW; 32-bit JDK needs 32-bit MinGW. Mismatched bitness will cause silent loading failures or link errors.
- Use the
4. Rule Out Typos & Simple Mistakes
- Double-check that your Java native method is declared correctly:
public native void sritest();(no typos in method name). - If you're using a package structure, the JNI method name must include underscore-separated package names (e.g.,
Java_com_example_Main_sritestfor acom.example.Mainclass).
5. Test with a Minimal Working Example
If you're still stuck, strip down your code to the simplest possible case to isolate the issue:
- Java code (
Main.java):public class Main { static { System.loadLibrary("testdll"); } public native void sritest(); public static void main(String[] args) { new Main().sritest(); } } - Compile to class:
javac Main.java - Generate header (JDK <10:
javah Main; JDK 10+:javac -h . Main.java) - C++ code (
Main.cpp):#include "Main.h" #include <iostream> JNIEXPORT void JNICALL Java_Main_sritest(JNIEnv* env, jobject obj) { std::cout << "JNI method executed successfully!" << std::endl; } - Compile DLL with the correct MinGW flags, then run
java Main. If this works, gradually add back your original code to find where the mismatch occurs.
内容的提问来源于stack exchange,提问作者Sri vishnu Bharat
相关产品推荐
相关产品推荐

