【发布时间】:2016-08-18 01:09:32
【问题描述】:
我正在尝试开发一个使用 c++ 代码的 java 程序。为此,我使用了 32 位 mingw、32 位 jre(版本 1.8.0_77)和 eclipse luna (64 位)。
问题
我在尝试加载 *.dll 时遇到问题。 (我还没有尝试运行任何方法)
我测试过的内容
这是我声明本地方法的类:
public class JniImports {
static{
System.loadLibrary("hello");
}
public int test(int n){
return testJava(n);
}
private native int testJava(int n);
}
这是 Main 方法:
public class Main {
public static void main(String[] args) {
JniImports a = new JniImports();
}
}
我已经在 Eclipse 上为 JVM 配置了参数,添加了这个:-Djava.library.path=./jni
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;
}
这是我用来编译的命令(g++ 是 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 并删除了 hello.dll 库,则会出现以下错误:
java.lang.UnsatisfiedLinkError: no hello in java.library.path
所以,用 32 位编译器编译 c++ 代码并使用 32 位 JRE,它可以找到 dll,但它还是崩溃了。
我也尝试在调试模式下编译 c++ 代码(使用 -g 标志),它也崩溃了。
我确定 dll 位于正确的位置,因为如果我将错误日志中显示的路径复制粘贴到窗口的资源管理器路径栏中,它会将我带到 dll 所在的文件夹,并且我'我确定它的名字是“hello.dll”。
【问题讨论】:
标签: java c++ eclipse dll java-native-interface