[英]Exception in thread “main” java.lang.UnsatisfiedLinkError: Native.initiate(I)V while running native dll from Java
This is the Native.cpp : 这是Native.cpp:
// 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];
}
and the Java class that implements and loads this native dll generated from the above Native.cpp file is like : 实现和加载从上述Native.cpp文件生成的本机dll的Java类如下所示:
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()
{
}
}
But while calling 但是在打电话时
(new Native()).initiate(1);
I get this run time error : 我收到此运行时错误:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Native.initiate(I)V 线程“主”中的异常java.lang.UnsatisfiedLinkError:Native.initiate(I)V
I have tried to rename _JAVA_initiate to JAVA_initiate and NATIVE_initiate and _JAVA_NATIVE_inititate and even JAVA_NATIVE_inititate, but It did not work still 我试图将_JAVA_initiate重命名为JAVA_initiate和NATIVE_initiate以及_JAVA_NATIVE_inititate甚至JAVA_NATIVE_inititate,但是它仍然无法正常工作
The library is loading perfectly fine, just while calling the native method, it is giving link error. 该库加载得很好,只是在调用本机方法时,它给出了链接错误。
EDIT: Below listed is the NativeC.h that is already included in the Native.cpp 编辑:下面列出的是NativeC.h,它已包含在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
You need to use javah
to generate the signature for your function, as the one you're using is missing a number of things. 您需要使用javah
为函数生成签名,因为您正在使用的签名缺少许多东西。 In particular, the JNI environment, which gets passed as an argument to every function. 特别是JNI环境,它作为参数传递给每个函数。
您需要实现在使用javah
生成的Native.h
中声明的方法,通常一个类有一个包,这是方法名的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.