JNI: Library is Found on Path, but Method is not (java.lang.UnsatisfiedLinkError)(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:\_libI 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.
这篇关于JNI:在路径上找到库,但未找到方法 (java.lang.UnsatisfiedLinkError)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JNI:在路径上找到库,但未找到方法 (java.lang.UnsatisfiedLinkError)
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01