3

从 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.
#
4

3 回答 3

3

You have added a package name since you generated the C code. The package name is now dll, but when you generated it, there wasn't one. Redo and adjust your C code accordingly so it agrees with the new .h file.

于 2013-10-09T07:52:14.777 回答
-1

删除 printf 并尝试从您的 cpp 文件中返回一些值或字符串,并尝试从 java 文件中打印它。

于 2014-11-25T09:04:16.520 回答
-1

尝试使用 64 位编译器进行编译,例如“x86_64-w64-mingw32-g++”。同样的错误发生在我身上,现在该错误已修复...

原因是另一个 java 实例可能正在您的机器上运行,或者系统架构与您的本机代码不匹配。尝试改变它,看看它是否有效。

于 2018-07-06T06:23:36.843 回答