线程"main" java.lang.UnsatisfiedLinkError: Native.initiate(I)V 从 Java 运行本机 dll 时
Exception in thread "main" java.lang.UnsatisfiedLinkError: Native.initiate(I)V while running native dll from Java
这是本机.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];
}
实现和加载从上述 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()
{
}
}
但是在打电话时
(new Native()).initiate(1);
我收到此运行时错误:
线程 "main" 中的异常 java.lang.UnsatisfiedLinkError: Native.initiate(I)V
我试图将_JAVA_initiate重命名为 JAVA_initiate 和 NATIVE_initiate 和 _JAVA_NATIVE_inititate甚至JAVA_NATIVE_inititate,但它仍然不起作用
库加载完美,就在调用本机方法时,它给出了链接错误。
编辑:下面列出的是已经包含在Native中的NativeC.h.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
您需要使用javah
来生成函数的签名,因为您使用的签名缺少许多内容。 特别是 JNI 环境,它作为参数传递给每个函数。
您需要实现使用javah
生成的Native.h
中声明的方法 通常,类有一个包,这是方法名称的一部分。
相关文章:
- 在这种情况下,java对象是否可以调用本机函数
- 使用本机 JNI 静态方法实现C++ Java 运行时错误
- Java 1.8 本机 System.load 通过 JNI 调用C++产生 TLSv1.如何获取 TLSv1.2?
- 如何在本机 c++ jni 中实现 java 类型数组
- Java 本机访问代码错误:"Invalid memory access"
- Android 无法从本机代码调用 Java 方法 JNI
- 将自定义 Java 数据模型传递给我的本机代码
- 如何使用 JNI 将 double 和无符号 int 从本机 c 库返回到 java
- 将本机C 代码转换为Java
- Java本机接口-C 不在等待Java功能完成
- 如何将PNG从Java类中传递给Android中的本机类
- 从本机代码返回"const char*"并在java中获取"字符串"
- 如何在Java中包含一个库,该库在C 中具有Java中具有本机绑定(库)的库
- 从java调用本机方法时使用Libc SIGSEGV
- 如何将 java 函数指针传递给 c++ 本机代码
- Java 本机接口 GetStringUTFChars 致命错误
- 使用 Java 本机接口时出错
- GraphicsMagick使用c++java本机代码的性能问题
- 无法对libhydro.dll运行java本机访问
- Java本机接口(JNI)是否受到c++ ABI兼容性问题的影响