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

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 (or javac -h for JDK 10+). For a void sritest() method in a top-level Main class, 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* and jobject parameters, and keep the method name case-sensitive (sritest must 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:
    System.out.println(System.getProperty("java.library.path"));
    
    If your DLL's directory isn't listed, either move the DLL to one of the listed directories, or launch your Java program with:
    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 include and include/win32 directories 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 -shared flag 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.

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_sritest for a com.example.Main class).

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:

  1. Java code (Main.java):
    public class Main {
        static {
            System.loadLibrary("testdll");
        }
        public native void sritest();
        public static void main(String[] args) {
            new Main().sritest();
        }
    }
    
  2. Compile to class: javac Main.java
  3. Generate header (JDK <10: javah Main; JDK 10+: javac -h . Main.java)
  4. 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;
    }
    
  5. 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:27:04