JNI:在路径上找到库,但方法不是(java.lang.UnsatisfiedLinkError) [英] JNI: Library is Found on Path, but Method is not (java.lang.UnsatisfiedLinkError)
问题描述
我试图使用JNI并获取java.lang.UnsatisfiedLinkError。不像其他百万个问题,关于这个问题,我有我的路径上的lib,甚至看到异常更改时,我删除它。
这是我的java类代码:
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产生的头文件的节略部分:
/ *
*类:com_Tune
*方法:add
*签名: b $ b * /
JNIEXPORT jint JNICALL Java_com_Tune_add
(JNIEnv *,jobject,jint,jint);
这是我的c ++代码:
#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获取的运行时异常:
线程main中的异常java.lang.UnsatisfiedLinkError:com.Tune.add(II)I
at com.Tune.add Native方法)
at com.Tune.main(Tune.java:9)
读取上面的异常意味着它DID找到库lala,但是方法add仍然没有定义。我看到的项目和教程之间唯一不同的是:
- Mine使用一个包,而不是默认包
- Mine有一个返回值。
- 我移动了我的dll它已创建(我不认为这会中断,因为我的路径配置。)
如何实现?
其他信息:
操作系统:Windows 7
JDK:1.6.0_31(对于x86,32位的jvm)
C ++ IDE:Code :: Blocks(DLL由Code :: Blocks IDE自动编译) >
C ++编译器:MinGW32-g ++(GNU C ++编译器)
我有C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ _\include
我在C:\_\lib中有lala.dll
环境变量:
PATH:C:\Program Files(x86)\NVIDIA Corporation \PhysX\Common;%CommonProgramFiles%\Microsoft Shared\Windows Live; C:\Program Files(x86)\AMD APP\\ \\ bin \x86_64; C:\Program Files(x86)\AMD APP\bin \x86;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT% \System32\WindowsPowerShell\v1.0\; C:\Program Files(x86)\ATI Technologies\ATI.ACE\Core-Static; C:\Apps;%JAVA_HOME%\bin ; C:\Program Files\MySQL\MySQL Server 5.5 \bin;%MAVEN_HOME%\bin;%HADOOP_INSTALL%\bin; c:\Program Files(x86)\ Microsoft SQL Server \100 \Tools\Binn \; c:\Program Files \ Microsoft SQL Server \100\Tools\Binn\; c:\Program Files \ Microsoft SQL Server \100\DTS\\ \\ Binn \; C:\MinGW\bin; C:\Program Files(x86)\GnuWin32\bin; C:_\path; C:\_\lib; C:\程序文件(x86)\Microsoft Visual Studio 10.0 \VC\bin; C:\_\include
只是猜测...你的dll依赖于另一个dll,不是在路径上? MinGW模块通常依赖于特定的C运行时库。
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.
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);
}
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);
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;
}
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)
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:
- Mine uses a package, instead of the default package (shouldn't tutorials really do this?!?! come on let's get professional)
- Mine has a return value.
- I moved my dll after it was created (I don't think this will break it since my path is configured.)
How is this possible?
Other Info:
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)
I have jni.h and com_Tune.h in C:\_\include
I have lala.dll in C:\_\lib
Environment Variables:
PATH: C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;%CommonProgramFiles%\Microsoft Shared\Windows Live;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Apps;%JAVA_HOME%\bin;C:\Program Files\MySQL\MySQL Server 5.5\bin;%MAVEN_HOME%\bin;%HADOOP_INSTALL%\bin;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\MinGW\bin;C:\Program Files (x86)\GnuWin32\bin;C:_\path;C:\_\lib;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin;C:\_\include
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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!