簡體   English   中英

java.lang.UnsatisfiedLinkError - JNI

[英]java.lang.UnsatisfiedLinkError - JNI

每次運行程序時,我都會收到java.lang.UnsatisfiedLinkError錯誤。 我有一個本機,一個包裝器,以及通過包裝器調用本機的程序。

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.cpp中

#include "main.h"

#include<windows.h>
#include<iostream>

using namespace std;

JNIEXPORT void JNICALL MsgBox(string text, string title)
{
    MessageBox(NULL, text.c_str(), title.c_str(), MB_OK);
}

extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            break;

        case DLL_PROCESS_DETACH:
            break;

        case DLL_THREAD_ATTACH:
            break;

        case DLL_THREAD_DETACH:
            break;
    }
    return TRUE;
}

Wrapper.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();
}
}

Main.Java

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

本機是使用MinGW 64位構建的。 無論如何,我不知道為什么我得到錯誤。 救命?????

我認為問題是你的JNI簽名不匹配。 那是:

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

應該是這樣的:

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

其中,java_com_example應替換為您的包名(。將在包名中替換為_)。

要么

我建議你使用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方法允許我們從“默認”路徑加載庫。

    的System.loadLibrary( “HelloWorld” 的);

  • System.load允許我們通過其絕對路徑從任何地方加載庫。
    System.load("c:/path/to/dll/HelloWorld.dll") ;

另一件可能導致該錯誤的原因是在編譯我們的cpp庫時缺少頭文件。 確保在cpp文件中包含標題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM