JNI UnsatisfiedLinkError:动态链接库(DLL)初始化例程失败 [英] JNI UnsatisfiedLinkError: A dynamic link library (DLL) initialization routine failed

查看:158
本文介绍了JNI UnsatisfiedLinkError:动态链接库(DLL)初始化例程失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过JNI从c ++调用一个函数.我已经按照我在网上找到的说明进行操作,但仍然出现异常:

I am trying to call a function from c++ via JNI. I have followed the instructions I found online and still get an Exception:

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

Exception in thread "main" java.lang.UnsatisfiedLinkError: \path\to\dll\remoteAPI.dll: A dynamic link library (DLL) initialization routine failed

DLL文件的路径正确,并且位于该文件中.我通过IntelliJ中的VMOptions通过以下路径添加了路径: -Djava.library.path = \ path \ to \ dll

The path to the DLL file is correct and it is located there. I added the path via the VMOptions in IntelliJ via: -Djava.library.path=\path\to\dll

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

So why do I still get an Exception? Apparently this exception is throws when the DllMain returns the value false. But do I need one here or has the jni-library one and if I need to implement it, where do I put it?

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) {

}

App.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而不是使用IDE Code :: Blocks 摆脱了异常.我在哪里使用的命令

I now got rid of the exception by compiling the dll from the command line instead of using the IDE Code::Blocks. The commands I used where

g ++ -c -I -I%JAVA_HOME%\ include -I%JAVA_HOME%\ include \ win32实体_RemoteAPI.cpp -o实体_RemoteAPI.o

g++ -c -I%JAVA_HOME%\include -I%JAVA_HOME%\include\win32 entities_RemoteAPI.cpp -o entities_RemoteAPI.o

g ++ -shared -o remoteAPI.dll实体_RemoteAPI.o -Wl,-add-stdcall-alias

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

显然,它与构建DLL时通过代码:: Blocks选项有关.

Apparently it has something to do with the options Code::Blocks passes when building the DLL.

这篇关于JNI UnsatisfiedLinkError:动态链接库(DLL)初始化例程失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆