JNI:在路径上找到库,但未找到方法 (java.lang.UnsatisfiedLinkError)

我正在尝试使用 JNI 并得到 java.lang.UnsatisfiedLinkError.与其他有关此问题的数百万个问题不同,我的路径上有这个库,甚至在我删除它时也看到了异常变化.我确定我创建的 dll 有问题,但我不确定是什么问题.

I'm trying to use JNI and getting java.lang.UnsatisfiedLinkError. Unlike the other million questions asked about this, I have the lib on my path, and have even seen the exception change when I remove it. I'm sure that something is wrong with the dll I have created, but I'm not sure what.

这是我的java类代码:

Here is my java class code:

package com;

public class Tune {
    static {
        System.loadLibrary("lala");
    }
    public static void main(String[] args) {
        Tune j = new Tune();
        System.out.println("2+6="+j.add(2, 6));
    }
    native public int add(int x,int y);
}

这是我的 javah 生成的头文件的删节部分:

Here is the abridged portion of my javah produced header file:

/*
 * Class:     com_Tune
 * Method:    add
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_com_Tune_add
  (JNIEnv *, jobject, jint, jint);

这是我的 C++ 代码:

Here is my c++ code:

#include <jni.h>
#include <com_Tune.h>

JNIEXPORT jint JNICALL Java_com_Tune_add
  (JNIEnv * env, jobject obj, jint x, jint y) {
    return x+y;
  }

这是我从 eclipse 得到的运行时异常:

Here is the runtime exception I get from eclipse:

Exception in thread "main" java.lang.UnsatisfiedLinkError: com.Tune.add(II)I
    at com.Tune.add(Native Method)
    at com.Tune.main(Tune.java:9)

我读到上面的异常意味着它确实找到了库lala",但仍然没有定义方法add".我发现我的项目和教程之间唯一不同的地方是:

I read that the above exception means it DID find the library "lala", but that the method "add" is still not defined. The only things I see different between my project and the tutorial are:

  • 我的使用的是包,而不是默认包(教程不应该真的这样做吗?!?!来吧,让我们变得专业)
    • 我的有一个返回值.
    • 我在创建 dll 后移动了它(我认为这不会破坏它,因为我的路径已配置.)

    这怎么可能?

    操作系统:Windows 7
    JDK:1.6.0_31(用于x86,32位jvm)
    C++ IDE: Code::Blocks (dll由Code::Blocks IDE自动编译)
    C++ 编译器:MinGW32-g++(GNU C++ 编译器)

    OS: Windows 7
    JDK: 1.6.0_31 (for x86, 32 bit jvm)
    C++ IDE: Code::Blocks (the dll was compiled automatically by the Code::Blocks IDE)
    C++ compiler: MinGW32-g++ (the GNU C++ compiler)

    我在 C:\_include 中有 jni.h 和 com_Tune.h
    我在 C:\_lib

    I have jni.h and com_Tune.h in C:\_include
    I have lala.dll in C:\_lib

    环境变量:
    路径:C:Program Files (x86)NVIDIA CorporationPhysXCommon;%CommonProgramFiles%Microsoft SharedWindows Live;C:Program Files (x86)AMD APPinx86_64;C:Program Files (x86)AMD APPinx86;%SystemRoot%system32;%SystemRoot%;%SystemRoot%System32Wbem;%SYSTEMROOT%System32WindowsPowerShellv1.0;C:Program Files (x86)ATI TechnologiesATI.ACECore-Static;C:Apps;%JAVA_HOME%in;C:Program FilesMySQLMySQL Server 5.5in;%MAVEN_HOME%in;%HADOOP_INSTALL%in;c:Program Files (x86)Microsoft SQL Server100ToolsBinn;c:Program FilesMicrosoft SQL Server100ToolsBinn;c:Program FilesMicrosoft SQL Server100DTSBinn;C:MinGWin;C:Program Files (x86)GnuWin32in;C:_path;C:\_lib;C:Program Files (x86)Microsoft Visual Studio 10.0VCin;C:\_包括

    Environment Variables:
    PATH: C:Program Files (x86)NVIDIA CorporationPhysXCommon;%CommonProgramFiles%Microsoft SharedWindows Live;C:Program Files (x86)AMD APPinx86_64;C:Program Files (x86)AMD APPinx86;%SystemRoot%system32;%SystemRoot%;%SystemRoot%System32Wbem;%SYSTEMROOT%System32WindowsPowerShellv1.0;C:Program Files (x86)ATI TechnologiesATI.ACECore-Static;C:Apps;%JAVA_HOME%in;C:Program FilesMySQLMySQL Server 5.5in;%MAVEN_HOME%in;%HADOOP_INSTALL%in;c:Program Files (x86)Microsoft SQL Server100ToolsBinn;c:Program FilesMicrosoft SQL Server100ToolsBinn;c:Program FilesMicrosoft SQL Server100DTSBinn;C:MinGWin;C:Program Files (x86)GnuWin32in;C:_path;C:\_lib;C:Program Files (x86)Microsoft Visual Studio 10.0VCin;C:\_include

    推荐答案

    只是猜测......你的 dll 是否依赖于另一个不在路径上的 dll?MinGW 模块通常依赖于特定的 C 运行时库.

    Just guessing... Is your dll depends on another dll that is not on the path? MinGW modules usually depend on specific C runtime library.

相关文章