I am trying to call a function from c++ via JNI. I have followed the instructions I found online and still get an Exception:
Exception in thread "main" java.lang.UnsatisfiedLinkError: \path\to\dll\remoteAPI.dll: A dynamic link library (DLL) initialization routine failed
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
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();
}
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%JAVA_HOME%\include -I%JAVA_HOME%\include\win32 entities_RemoteAPI.cpp -o entities_RemoteAPI.o
and
g++ -shared -o remoteAPI.dll entities_RemoteAPI.o -Wl,--add-stdcall-alias
Apparently it has something to do with the options Code::Blocks passes when building the DLL.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.