简体   繁体   中英

JNI C++ UnsatisfiedLinkError When Calling Method

I have been trying to create a simple JNI Test with C++. For Java I am using Eclipse, and for C++ I am using Visual Studio 2013. I followed the directions on other pages on StackOverFlow but nothing really seems to work. The error occurs when I try calling the native method in java listed in C++.

Thanks for your time.

Header.h

#include <jni.h>;
using namespace std;
#ifndef Header
#define Header
JNIEXPORT void JNICALL Java_base_Main_print(JNIEnv *env, jobject obj);
#endif

Edited Header.h (solution which worked)

#include <jni.h>;
using namespace std;

#ifndef Header
#define Header

extern "C" {
JNIEXPORT void JNICALL Java_base_Main_print(JNIEnv *env, jobject obj);
}
#endif

JNIHelloWorld.cpp

#include "Header.h";

JNIEXPORT void JNICALL Java_base_Main_print(JNIEnv *env, jobject obj) {
    printf("This is a JNI tester\n");
    //return;
}

JavaCode

package base;
public class Main {
    static {System.loadLibrary("JNIHelloWorld");}
    public static void main(String args[]){
        Main m = new Main();
        m.print();
    }
    public native void print();
}

Error

Exception in thread "main" java.lang.UnsatisfiedLinkError: base.Main.print()V
    at base.Main.print(Native Method)
    at base.Main.main(Main.java:9)

try #extern "c" { } around the JNI code.

Verify by looking at the exports of your DLL to see that the C++ name mangling did not get involved.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM