2013-05-13 81 views
2

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

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位構建的。無論如何,我不明白爲什麼我會得到這個錯誤。幫幫我?????

回答

10

我認爲問題在於您的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應該與你的包名來替換(是代之以_在包名)。

OR

我會建議你使用JAVAH在Java -jni選項可用來生成你的本地函數簽名和聲明。

+1

是的,我只是有一個類似的問題。試圖從另一個jni項目中使用lib,但由於我使用的是與原始項目不同的軟件包名稱,因此不斷得到此UnsatisfiedLinkError。在新項目中使用原始軟件包名稱並解決問題。 – 2014-10-30 14:04:56

+0

謝謝你,你的解決方案解決了我的問題。 – Harshil 2016-12-19 13:26:01

2

如果用

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");

相關問題