【发布时间】:2020-07-31 22:08:26
【问题描述】:
当我在 Java 中测试 JNI 时出现以下显式错误:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Italk2learn.hello()V
at Italk2learn.hello(Native Method)
at Italk2learn.main(Italk2learn.java:10)
dll或路径没有问题,因为我的java类的静态代码运行良好:
static {
try {
System.loadLibrary("Italk2learn");
} catch (Exception e) {
System.err.println(e);
System.exit(1);
}
}
而且我认为它使图书馆很好。
我使用 JVM 32 位编译并获取 C++ 头文件 (javah) 和用于 C++ 的 MinGW32。在这两种情况下,我都将 eclipse 用于 C++ 和 Java。
这是我的代码:
Java:
public class Italk2learn {
public native void hello();
public static void main(String[] args) {
System.out.println("Hello World Java!");
try {
new Italk2learn().hello();
} catch (Exception e) {
System.err.println(e);
System.exit(1);
}
}
static {
try {
System.loadLibrary("Italk2learn");
} catch (Exception e) {
System.err.println(e);
System.exit(1);
}
}
}
C++:
#include <jni.h>
#include <stdio.h>
#include "italk2learn.h"
JNIEXPORT void JNICALL Java_Italk2learn_hello(JNIEnv *, jobject) {
printf("Hello World C++!\n");
#ifdef __cplusplus
printf("__cplusplus is defined\n");
#else
printf("__cplusplus is NOT defined\n");
#endif
return;
}
【问题讨论】:
-
您是如何构建 DLL 的?如果您查看
Italk2learn.DLL它有多大,它在您的 PATH 中吗? -
我在 Eclipse 中使用 GCC C++ 编译器。大小为 75 KB(包括 MinGW 和 JNI),是的,它在我的环境变量的 windows 路径中。
标签: java c++ dll java-native-interface