Search code examples
javaclinkerjava-native-interface

Can't find JNI_OnLoad_libname in example program


How exactly do I call native static libraries from Java? I'm using Java 8.

It looks to me like I should be able to define a JNI_OnLoad_library in a C++ program with an embedded JVM but my VM keeps dying when I call System.loadLibrary. The following only prints "Hello from main".

main.cpp:

#include <jni.h>
#include <iostream>

extern "C" {
    JNIEXPORT jint JNI_OnLoad_hello(JavaVM *vm, void *reserved) {
        std::cout << "Hello World" << std::endl;
        return JNI_VERSION_1_8;
    }
}

int main(int argc, char** argv) {
    JavaVM *jvm;
    JNIEnv *env;
    JavaVMInitArgs vm_args;
    jint error;

    JavaVMOption* options = new JavaVMOption[argc-1];
    for(int i = 0;i < argc-1;i++) {
        options[i].optionString = argv[i+1];
    }

    vm_args.nOptions = argc-1;
    vm_args.options = options;

    vm_args.version = JNI_VERSION_1_8;

    JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
    jclass cls = env->FindClass("Main");
    jclass stringCls = env->FindClass("java/lang/String");
    jmethodID mid = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
    jobjectArray mainArgs = env->NewObjectArray(0, stringCls, NULL);
    env->CallStaticVoidMethod(cls, mid, mainArgs);
    jvm->DestroyJavaVM();
}

Main.java:

class Main {
    public static void main(String[] args) {
        try {
            System.out.println("Hello from main");
            System.loadLibrary("hello");
            System.out.println("Hello after main");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Solution

  • I was finally able to get the JVM to find JNI_OnLoad_hello by passing --export-dynamic to the linker. This allows shared libraries to resolve symbols by looking for them in the executable. Also, System.loadLibrary was throwing UnsatisfiedLikeErrors like I suspected but for some reason I have to handle them in JNI using CheckException and DescribeException.