从 java 调用 dll 时出现此错误
Exception in thread "main" java.lang.UnsatisfiedLinkError: dll.HelloJNI.sayHello()V
at dll.HelloJNI.sayHello(Native Method)
at dll.HelloJNI.main(HelloJNI.java:7)
这是我的java代码
public class HelloJNI {
public static void main(String[] args) {
HelloJNI h = new HelloJNI();
h.sayHello(); // invoke the native method
}
static {
try{
System.load("D://Program Files//Java//jdk1.7.0_40//bin//hello.dll"); // hello.dll (Windows) or libhello.so (Unixes)
}
catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
private native void sayHello();
}
这是我的 dll 的 C 代码。
我正在使用gcc编译器生成dll
我正在使用 MinGWC
gcc -Wl,--add-stdcall-alias -I"\include"-I"\include\win32"-shared -o hello.dll HelloJNI.c
#include <jni.h>
#include <stdio.h>
#include "HelloJNI.h"
JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) {
printf("Hello World!\n");
return;
}
我已经删除了包 dll 并且在执行时出现了这个错误
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x610d70b4, pid=1720, tid=1160
#
# JRE version: Java(TM) SE Runtime Environment (7.0_40-b43) (build 1.7.0_40-b43)
# Java VM: Java HotSpot(TM) Client VM (24.0-b56 mixed mode, sharing windows-x86 )
# Problematic frame:
# C [cygwin1.dll+0xd70b4]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
最佳答案
自生成 C 代码以来,您已经添加了包名称。包名现在是 dll,但是当你生成它时,没有。重做并相应地调整您的 C 代码,使其与新的 .h 文件一致。
关于java.lang.UnsatisfiedLinkError dll.HelloJNI.sayHello()V,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19265579/