AJavaProgrammer
AJavaProgrammer

Reputation: 41

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)

Upvotes: 2

Views: 1248

Answers (1)

Raindog
Raindog

Reputation: 1488

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.

Upvotes: 3

Related Questions