java.lang.UnsatisfiedLinkError dll.HelloJNI.sayHello()V

标签 java c++ dll unsatisfiedlinkerror

从 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/

相关文章:

java - BeanUtils 日期转换问题

c++ - 这是使用 alloca 的好理由吗?

c++ - 如何遍历 std::list<MyClass *> 并从迭代器获取该类中的方法?

c++ - ARM 中 16 位大端到小端的快速转换

c# - 获取调用窗口的窗口句柄

c++ - 将 std::string 传递给 C++ DLL 以便与 C# dll 一起使用

java - Wildfly 9.0.0 和 Netbeans 8.0.2

java - 从 Swing 接收数据的 servlet 在浏览器上显示结果

java - 如何在自动完成地点谷歌地图API中创建边界(仅特定地点)

c# - 在不重新编译整个应用程序的情况下升级依赖的 DLL