JNI 和 UnsatisfiedLinkError
我正在使用 JNI 执行第一步,并尝试编写一个简单的 Hello Java 程序,但它失败并出现以下错误:
Exception in thread "main" java.lang.UnsatisfiedLinkError: HelloJava.dostuff()V
at HelloJava.dostuff(Native Method)
at HelloJava.main(HelloJava.java:12)
这是我的 Java 类:
class HelloJava {
private native void dostuff();
static {
System.loadLibrary("HelloJavaDLL");
}
public static void main(String[] args) {
System.out.println("This is from java.");
HelloJava j = new HelloJava();
j.dostuff();
}
}
HelloJava.c 是使用 javah 生成的-jni HelloJava。
C 实现如下所示:
#include <stdio.h>
#include <jni.h>
#include "HelloJava.h"
JNIEXPORT void JNICALL Java_HelloJava_dostuff
(JNIEnv *env, jobject this)
{
printf("And this comes from C ! :)\n");
}
我在 Windows 上使用 gcc 将其编译为共享库 (.dll)。
现在运行 Java .class 文件会发生上述异常。 你能告诉我为什么会出现这个错误吗?
顺便说一句,你能告诉我如何在 C++ 中使用 JNI 吗?
UPDATE
也许你想自己尝试一下?我真的找不到这个问题。这里是 MediaFire 的链接,您可以在其中下载 .包含所有文件的 zip 文件(即使是已编译的文件)。
重试了一切,但仍然是同样的问题。
这些是我所做的步骤:
- 编写 Hello.java
- 使用
javac Hello.java
编译 Hello.java - 使用
javah -jni Hello
创建头文件 - 编写 Hello.c 文件
- 编译Hello.c 文件使用
gcc Hello.c -shared -o Hello.dll -I"C:\Java\jdk1.7.0\include" -I"C:\Java\jdk1.7.0\include\win32"
- 执行Hello .class 使用
java Hello
谢谢。
解决方案
根据此问题在这里。
感谢AH的帮助!
I'm doing my first steps with JNI and tried to write a simple Hello Java program, but it fails with this error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: HelloJava.dostuff()V
at HelloJava.dostuff(Native Method)
at HelloJava.main(HelloJava.java:12)
This is my Java class:
class HelloJava {
private native void dostuff();
static {
System.loadLibrary("HelloJavaDLL");
}
public static void main(String[] args) {
System.out.println("This is from java.");
HelloJava j = new HelloJava();
j.dostuff();
}
}
The HelloJava.c is generated using javah -jni HelloJava
.
The C implementation looks like this:
#include <stdio.h>
#include <jni.h>
#include "HelloJava.h"
JNIEXPORT void JNICALL Java_HelloJava_dostuff
(JNIEnv *env, jobject this)
{
printf("And this comes from C ! :)\n");
}
I compiled it on Windows using gcc to a shared library (.dll).
Now running the Java .class File the Exception from above occurs. Can you tell me why this error appears ?
And by-the-way, can you tell me how I can use JNI with C++ ?
UPDATE
Maybe you want to try it yourself ? I really can't find the issue.Here is a link to MediaFire where you can download a .zip file containing all files (event the compiled ones).
The retried everything but it's still the same issue.
Theese are the steps I did:
- Write Hello.java
- Compile Hello.java using
javac Hello.java
- Create a header file using
javah -jni Hello
- Write the Hello.c file
- Compile the Hello.c file using
gcc Hello.c -shared -o Hello.dll -I"C:\Java\jdk1.7.0\include" -I"C:\Java\jdk1.7.0\include\win32"
- Execute Hello.class using
java Hello
Thanks.
SOLUTION
Adding -Wl,--kill-at
to the gcc command fixes the problem, according to this question here.
Thanks to A.H. for his help !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请检查:
HelloJavaDLL.dll
(在 Windows 上)PATH
环境变量)中或提供的使用-Djava.library.path=C:\WhereEverItIs
到java
。第二个问题:JNI 开箱即用地支持 C 和 C++。如果您查看生成的头文件和
jni.h
文件,您将看到这一点。Please check:
HelloJavaDLL.dll
(on Windows)PATH
environment variable) or supplied tojava
with-Djava.library.path=C:\WhereEverItIs
.The second question: JNI supports both C and C++ right out of the box. If you look into the generated header file and in the
jni.h
file you will see this.