2012-11-06 97 views
2

運行本機DLL V本是Native.cpp:異常線程 「main」 java.lang.UnsatisfiedLinkError中:Native.initiate(I),而從Java

// Native.cpp : Defines the exported functions for the DLL application. 
// 
#include "stdafx.h" 
#define ALLEGRO_NO_MAGIC_MAIN 
#include <stdio.h> 
#include <string> 
#include <windows.h> 
#include "generic_interface.h" 
#include "NativeC.h" 

using namespace std; 

// Some useful defines I liked from Sun's stuff 
#define JNIEXPORT __declspec(dllexport) 
#define JNICALL __cdecl 
#define jint long 


typedef ExportedClass* (__cdecl *exported_class)(); 
HINSTANCE temptDLL; 
ExportedClass** importedClasses; 
char** classNamePerIndex; 
int libraryLength = 0; 

JNIEXPORT void JNICALL _JAVA_initiate(HNative *self, jint libraryLength) { 
    importedClasses = new ExportedClass*[libraryLength]; 
    classNamePerIndex = new char*[libraryLength]; 
} 

和Java類,它實現和負載從上面的Native.cpp文件生成此機dll是這樣的:

public class Native { 
    // guess? 
    native public void initiate(int libraryLength); 


    // Loads the file Native.DLL at run-time 
    static { 
    System.loadLibrary("Native"); 
    } 

    // Constructor 
    public Native() 
    { 
    } 

} 

但同時呼籲

(new Native()).initiate(1); 

我得到這個運行時錯誤:

在線程異常「主要」 java.lang.UnsatisfiedLinkError中:Native.initiate(我)V

我試圖_JAVA_initiate重命名爲JAVA_initiate和NATIVE_initiate和_JAVA_NATIVE_inititate甚至JAVA_NATIVE_inititate ,但它仍然沒有工作

該庫加載完美,只是在調用本地方法時,它給出了鏈接錯誤。

編輯:下面列出的是已包含在Native.cpp

/* DO NOT EDIT - automatically generated by javah */ 
#include "Native.h" 

/* Header for class Native */ 

#ifndef _Included_Native 
#define _Included_Native 

typedef struct ClassNative { 
#pragma pack(push,4) 
    int32_t MSReserved; 
    struct Hjava_lang_String * string_; 
    /*boolean*/ long boolean_; 
    /*byte*/ long byte_; 
    /*char*/ long char_; 
    double double_; 
    float float_; 
    long int_; 
    int64_t long_; 
    /*short*/ long short_; 
    struct Hjava_lang_String * w; 
    long x; 
    long y; 
#pragma pack(pop) 
} ClassNative; 
#define HNative ClassNative 

#ifdef __cplusplus 
extern "C" { 
#endif 
__declspec(dllexport) void __cdecl _JAVA_initiate (struct HNative *, long); 
__declspec(dllexport) void __cdecl _JAVA_loadLibraryAndInitiate (struct HNative *, struct  Hjava_lang_String *); 
__declspec(dllexport) long __cdecl _JAVA_evaluateLibrary (struct HNative *, struct    Hjava_lang_String *, struct Hjava_lang_String *); 
#ifdef __cplusplus 
} 
#endif 
#endif 

回答

0

您需要使用javah來生成你的函數簽名的NativeC.h,因爲你正在使用的一個是缺少一些東西。特別是,JNI環境作爲每個函數的參數傳遞。

+0

嗨,我以前沒有在問題中包含該文件,但現在我已經更新了它。我使用HNative而不是JNI,因爲這個http://www.pacifier.com/~mmead/cs510jip/jni/文章建議使用Windows的HNative。 – user1803112

0

您需要實現使用javah生成的Native.h中聲明的方法通常,類有一個包,這是方法名的一部分。

+0

嗨,我沒有在前面的問題中包含該文件,但我確實包含了聲明瞭所有函數的NativeC.h。 – user1803112

+0

如果你定義了'.h'文件中聲明的方法,它將會工作。 –

+0

如果你這樣做,但仍然失敗,你有一個不同的問題。例如,您無法將32位DLL加載到64位JVM中。 –

相關問題