2013-05-13 46 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");

相关问题