我试图通过JNI从c++调用一个函数。我遵循了我在网上找到的说明,但还是有例外:
线程“主”
中的java.lang.UnsatisfiedLinkError异常:\path\to\dll\remoteAPI.dll:动态链接库初始化例程失败
DLL文件的路径是正确的,并且它位于那里。我在-Djava.library.path=\path\to\dll:VMOptions中添加了通过IntelliJ的路径
为什么我还会有例外呢?显然,此异常是在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
#endifentities_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();
}发布于 2019-09-26 09:25:32
现在,我通过从命令行编译dll来消除异常,而不是使用IDE 代码::块。我使用的命令
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
显然,它与选项代码::块在构建DLL时传递有关。
https://stackoverflow.com/questions/58113444
复制相似问题