2

I get the following explicit error when I test JNI in Java:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Italk2learn.hello()V
    at Italk2learn.hello(Native Method)
    at Italk2learn.main(Italk2learn.java:10)

There's no problem with the dll or paths because the static code of the my java class works well:

static {
        try {
            System.loadLibrary("Italk2learn");
        } catch (Exception e) {
            System.err.println(e);
            System.exit(1);
        }
    }

And I think that it gets the library fine.

I use JVM 32 bits to compile and get the C++ header(javah) and MinGW32 for C++. In both cases I use eclipse for C++ and Java.

This is my code:

Java:

public class Italk2learn {

    public native void hello();

    public static void main(String[] args) {
        System.out.println("Hello World Java!");
        try {
            new Italk2learn().hello();
        } catch (Exception e) {
            System.err.println(e);
            System.exit(1);
        }
    }
    static {
        try {
            System.loadLibrary("Italk2learn");
        } catch (Exception e) {
            System.err.println(e);
            System.exit(1);
        }
    }
}

C++ :

#include <jni.h>
#include <stdio.h>
#include "italk2learn.h"

JNIEXPORT void JNICALL Java_Italk2learn_hello(JNIEnv *, jobject) {
    printf("Hello World C++!\n");
#ifdef __cplusplus
    printf("__cplusplus is defined\n");
#else
    printf("__cplusplus is NOT defined\n");
#endif
    return;
}
2
  • How did you build your DLL? If you look at the Italk2learn.DLL how big is it and is it in your PATH? Commented Jul 30, 2013 at 17:57
  • 1
    I use GCC C++ Compiler with Eclipse. The size is 75 KB (includes MinGW and JNI) and yes, it's in my windows path of environtment variables. Commented Jul 31, 2013 at 14:07

2 Answers 2

4

I have found the answer! It seems that when using JNI in Windows it looks for a function starting with _Java_ while in every other platform it looks for Java_. Why this is the case and not written in the documentation I don't know but it make everything work perfectly!

When javah creates the header file each function is named like Java_package_Class_method. The odd thing is that when compiled like this, JNI cannot find the right function for the native method and spits out errors, but if an underscore was added before Java then JNI would be able to find the function.

Sign up to request clarification or add additional context in comments.

Comments

3

If you're compiling with C++, you have to wrap your JNI methods with extern "C" to ensure that the compiler doesn't apply its own mangling:

extern "C" {
    JNIEXPORT void JNICALL Java_Italk2learn_hello(JNIEnv *, jobject) {
        // ..
    }
}

See: http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/design.html#wp224

1 Comment

javah already does that in the declaration generated into the .h file, and including the .h file in the .cpp file applies it to the definition from the declaration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.