[英]JNI and UnsatisfiedLinkError
I'm doing my first steps with JNI and tried to write a simple Hello Java program, but it fails with this error: 我正在使用JNI进行第一步,并尝试编写一个简单的Hello Java程序,但是由于以下错误而失败:
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: 这是我的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();
}
}
The HelloJava.c is generated using javah -jni HelloJava
. javah -jni HelloJava
是使用javah -jni HelloJava
生成的。
The C implementation looks like this: 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");
}
I compiled it on Windows using gcc to a shared library (.dll). 我在Windows上使用gcc将其编译为共享库(.dll)。
Now running the Java .class File the Exception from above occurs. 现在运行Java .class文件,发生上面的异常。 Can you tell me why this error appears ? 您能告诉我为什么出现此错误吗?
And by-the-way, can you tell me how I can use JNI with C++ ? 而且,您能告诉我如何在C ++中使用JNI吗?
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). 我真的找不到问题。这里是MediaFire的链接,您可以在其中下载包含所有文件的.zip文件(如果已编译)。
The retried everything but it's still the same issue. 重试了所有内容,但仍然是相同的问题。
Theese are the steps I did: Theese是我执行的步骤:
javac Hello.java
使用javac Hello.java
编译javac Hello.java
javah -jni Hello
使用javah -jni Hello
创建头文件 gcc Hello.c -shared -o Hello.dll -I"C:\\Java\\jdk1.7.0\\include" -I"C:\\Java\\jdk1.7.0\\include\\win32"
java Hello
使用java Hello
执行Hello.class Thanks. 谢谢。
Adding -Wl,--kill-at
to the gcc command fixes the problem, according to this question here. 根据此处的此问题,在gcc命令中添加-Wl,--kill-at
可解决此问题 。
Thanks to AH for his help ! 感谢AH的帮助!
Please check: 请检查:
HelloJavaDLL.dll
(on Windows) 库的文件名是HelloJavaDLL.dll
(在Windows上) PATH
environment variable) or supplied to java
with -Djava.library.path=C:\\WhereEverItIs
. DLL的目录位于库搜索路径(即PATH
环境变量)中,或通过-Djava.library.path=C:\\WhereEverItIs
提供给java
。 The second question: JNI supports both C and C++ right out of the box. 第二个问题:JNI 支持 C和C ++开箱的。 If you look into the generated header file and in the jni.h
file you will see this. 如果查看生成的头文件和jni.h
文件,您将看到此信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.