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

时间:2013-10-09 07:21:12

标签: 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.
#

3 个答案:

答案 0 :(得分:3)

您在生成C代码后添加了包名称。包名称现在是dll,但是当你生成它时,没有一个。重做并相应地调整您的C代码,使其与新的.h文件一致。

答案 1 :(得分:-1)

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

答案 2 :(得分:-1)

请尝试使用“ x86_64-w64-mingw32-g ++”之类的64位编译器进行编译。对我来说,发生了同样的错误,并且现在已修复此错误...