java.lang.UnsatifiedLinkError-JNI

java.lang.UnsatifiedLinkError-JNI,java,java-native-interface,mingw,native,Java,Java Native Interface,Mingw,Native,每次运行程序时,我都会收到一个java.lang.UnsatifiedLinkError错误。我有一个本机、一个包装器和通过包装器调用本机的程序 main.h #ifndef __MAIN_H__ #define __MAIN_H__ #include <windows.h> #ifdef BUILD_DLL #define DLL_EXPORT __declspec(dllexport) #else #define DLL_EXPORT __declspec(dl

每次运行程序时,我都会收到一个java.lang.UnsatifiedLinkError错误。我有一个本机、一个包装器和通过包装器调用本机的程序

main.h

#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>
#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif

#include<jni.h>
#include<iostream>

using namespace std;

extern "C"
{

JNIEXPORT void JNICALL native_MessageBox(string text, string title);

}

#endif
Main.Java

public class Wrapper
{
   private static File nativeFile = null;

       static
   {
            //ArchitectureReader and OSReader are classes I made to read the CPU              
            //bits and the OS
    ArchitectureType archType = ArchitectureReader.getArcitecture();
    OSType osType = OSReader.getOS();

    String filename = "C:/native-";

    if (osType == OSType.Windows)
    {
        if (archType == ArchitectureType.BITS_32)
            filename += "win32";
        else if (archType == ArchitectureType.BITS_64)
            filename += "win64";
        else
            System.exit(1);
    }
    else
        System.exit(1);

    nativeFile = new File(filename + ".dll");
    if (!nativeFile.exists())
        System.exit(1);

    System.load(filename); //This is where the Exception is thrown
}

private static native void native_MessageBox(String text, String title);
public static void MesageBox(String text, String title)
{
    native_MessageBox(text, title);
}

public static String getNativePath()
{
    return nativeFile.getAbsolutePath();
}
}
public class Main
{
public static void main(String[] args)
{
    Wrapper.MessageBox("Testing JNI", "JNI");
}
}

本机是使用MinGW 64位构建的。不管怎样,我不明白为什么我会出错。帮助????

我认为问题在于您的JNI签名不匹配。 即:

应该是这样的:

JNIEXPORT void JNICALL java_com_example_Wrapper_native_MessageBox(string text, string title);
其中,java_com_示例应替换为包名(.to replace为包名中的u)

我建议您使用java中可用的javah-jni选项生成本机函数签名和声明。

如果您使用

nativeFile = new File(filename + ".dll");
    if (!nativeFile.exists())
        System.exit(1);
你应该使用它

System.load(nativeFile);
将本机库加载到正在运行的Java程序中有两种不同的方法:

  • System.loadLibrary(String)
    System.load(String)

  • System.loadLibrary
    方法允许我们从“默认”路径加载库

    加载库(“HelloWorld”)

  • System.load
    允许我们通过其绝对路径从任何地方加载库
    System.load(“c:/path/to/dll/HelloWorld.dll”)


是的,我也有类似的问题。试图使用另一个jni项目中的库,但由于使用的包名与原始项目不同,因此一直出现此不满意的链接错误。在新项目中使用了原始包名称,问题已解决。
nativeFile = new File(filename + ".dll");
    if (!nativeFile.exists())
        System.exit(1);
System.load(nativeFile);