我想开发一个使用C++代码的java程序。为此,我使用32位mingw,32位jre(版本1.8.0_77)和eclipse luna (64位)。使用Java本机接口的错误
问题
我试图加载* .dll时出现问题。 (我还没有尝试运行还没有任何方法)
我已经测试
这是我所声明的本地方法的类:
public class JniImports {
static{
System.loadLibrary("hello");
}
public int test(int n){
return testJava(n);
}
private native int testJava(int n);
}
而这是主要的方法: -Djava.library.path=./jni
public class Main {
public static void main(String[] args) {
JniImports a = new JniImports();
}
}
我已经在蚀添加此配置为JVM的参数
c-header文件已由javah
工具生成,所以我确定没关系。无论如何,正如我所说的,我没有试图执行任何方法。在这里,头文件:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_jnitest_JniImports */
#ifndef _Included_com_jnitest_JniImports
#define _Included_com_jnitest_JniImports
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_jnitest_JniImports
* Method: testJava
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_com_jnitest_JniImports_testJava
(JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif
这里cpp文件:
#include "com_jnitest_JniImports.h"
JNIEXPORT jint JNICALL Java_com_jnitest_JniImports_testJava
(JNIEnv *env, jobject _this, jint n){
return 12;
}
下面是我使用编译命令(克++是mingw的32比特): g++ -Wl,--add-stdcall-alias -I"%JDK32_HOME%\include" -I"%JDK32_HOME%\include\win32" -shared -o hello.dll com_jnitest_JniImports.cpp
错误
如果我使用64位JRE,错误是它无法在64位jre上加载32位dll:
java.lang.UnsatisfiedLinkError: D:\ws\testjni\jni\hello.dll Can't load IA 32-bit .dll on a AMD 64-bit platform
我认为这个错误表明,在这种情况下,JRE发现* .dll。
如果我使用一个32位的JRE,错误在于,它无法找到的* .dll:
java.lang.UnsatisfiedLinkError: D:\ws\testjni\jni\hello.dll: Can't find dependent libraries
如果我使用一个32位JRE和我删除打招呼。 dll库,这是错误: java.lang.UnsatisfiedLinkError: no hello in java.library.path
因此,用32位编译器编译C++代码并使用32位JRE,它可以找到dll,但它仍然崩溃。
我也试着在调试模式下(用-g标志)编译C++代码,它也崩溃了。
我敢肯定,dll是在正确的位置,因为如果我复制粘贴窗口的资源管理器路径栏中的错误日志中显示的路径,它会带我到dll所在的文件夹,我肯定它的名字是“hello.dll”。