JNI 不满意链接错误: 动态链接库 (DLL) 初始化例程失败

JNI UnsatisfiedLinkError: A dynamic link library (DLL) initialization routine failed

本文关键字:DLL 初始化 例程 失败 动态链接库 不满意 链接 错误 JNI      更新时间:2023-10-16

我正在尝试通过JNI从c ++调用函数。 我已经按照我在网上找到的说明进行操作,但仍然收到异常:

线程"main"java.lang.UnsatisfiedLinkError中的异常:\path\to\dll\remoteAPI.dll:动态链接库(DLL(初始化例程失败

DLL 文件的路径是正确的,它位于那里。 我通过IntelliJ中的VMOptions通过以下方式添加了路径:-Djava.library.path=\path\to\dll

那么为什么我仍然会收到异常呢?显然,当 DllMain 返回值 false 时会抛出此异常。但是我在这里需要一个还是有 jni 库一个,如果我需要实现它,我应该把它放在哪里?

entities_remoteAPI.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class entities_remoteAPI */
#ifndef _Included_entities_remoteAPI
#define _Included_entities_remoteAPI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:     entities_remoteAPI
* Method:    sayHello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_entities_remoteAPI_sayHello
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif

entities_remoteAPI.cpp

#include <jni.h>
#include "entities_remoteAPI.h"

JNIEXPORT void JNICALL Java_entities_remoteAPI_sayHello
(JNIEnv* env, jobject thisObject) {
}

应用.java

public class App 
{
public static void main( String[] args ) {
System.out.println( "Hello World!" );
System.loadLibrary("remoteAPI");
RemoteAPI ai = new RemoteAPI();
ai.sayHello();
}
}

entities/RemoteAPI.java

package entities;
public class RemoteAPI {
public native void sayHello();
}

我现在通过从命令行编译 dll 而不是使用 IDECode::Blocks来摆脱异常。 我使用的命令在哪里

g++ -c -I%JAVA_HOME%\include -

I%JAVA_HOME%\include\win32 entities_RemoteAPI.cpp -o entities_RemoteAPI.o

g++ -shared -o remoteAPI.dll entities_RemoteAPI.o -Wl,--add-stdcall-alias

显然,它与构建DLL时的选项Code::Blocks传递有关。