2012-11-06 47 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中。 –

相关问题