2015-11-03 56 views
0

嘗試創建一個簡單的JNI程序,但總是得到異常: 線程「main」中的異常java.lang.UnsatisfiedLinkError:libLearningAccessJNI.sayHello()當調用裏面的方法時。C++/Java JNI得到異常unsastisfiedLinkError

下面是我的Java代碼的快照:

public class LearningAccessJava 
{ 
    static 
    { 
     System.load("C:/vob/Debug/libLearningAccessJNI.dll"); 
    } 

    // Declare native method 
    private native void sayHello(); 

    // Test Driver 
    public static void main(String[] args) 
    { 
     try 
     { 
      LearningAccessJava testJava = new LearningAccessJava(); 
      testJava.sayHello(); 
     } 
     catch (UnsatisfiedLinkError e) 
     { 
      // Always get this exception 
     } 
    } 
} 

的libLearningAccessJNI.dll的是,我通過編譯這個CPP和小時以下代碼創建的DLL。 這是我的C++ LearningAccessJNI.cpp和h文件

#include <jni.h> 
#include <iostream> 
#include <ostream> 
#include <stdio.h> 
#include "LearningAccessJava.h" 

using namespace std; 

JNIEXPORT void JNICALL Java_LearningAccessJava_sayHello(JNIEnv *env, jobject thisObj) 
{ 
    printf("Hello World TEst\n"); 
    //cout << "TEST TEST " << endl; 
    return; 
} 

的快照,這是我的LearningAccessJava.h文件,我使用JAVAH創建。

/* DO NOT EDIT THIS FILE - it is machine generated */ 
#include <jni.h> 
/* Header for class LearningAccessJava */ 

#ifndef _Included_LearningAccessJava 
#define _Included_LearningAccessJava 
#ifdef __cplusplus 
extern "C" { 
#endif 
/* 
* Class:  LearningAccessJava 
* Method: sayHello 
* Signature:()V 
*/ 
JNIEXPORT void JNICALL Java_LearningAccessJava_sayHello 
    (JNIEnv *, jobject); 

#ifdef __cplusplus 
} 
#endif 
#endif 

回答

0

你能否給我們提供用於編譯cpp文件的gcc命令行?確保:

  • 您使用命令行選項-Wl,--add-stdcall-alias-shared
  • 輸出.dll文件的路徑實際上是"C:/vob/Debug/libLearningAccessJNI.dll"
+0

移動網絡運營商您好,非常感謝。命令行選項是我錯過的東西。它現在有效。 – nsutanto

+0

很高興我能幫到你。 – MNos