Skip to content Skip to sidebar Skip to footer

How To Call Java Function From C

I'm stuck with this, I need to call a Java Function from c/c++. In the examples and tutorials i only see a java app calling a c method, and in this same method calling another java

Solution 1:

You can't reuse JNIEnv because it is specific to the calling thread. To call (non-static) Java method from the native code, you need something like this:

static JavaVM *gJavaVM;
static jobject gCallbackObject = NULL;

JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
    gJavaVM = vm;
    return JNI_VERSION_1_6;
}

JNIEXPORT void JNICALL JNI_FUNCTION(AndroidActivity_nativeInit)(JNIEnv* env, jobject obj, int width, int height) {
    // ...
    gCallbackObject = (*env)->NewGlobalRef(env, obj);
}

JNIEXPORT void JNICALL JNI_FUNCTION(AndroidActivity_nativeRelease)(JNIEnv* env, jobject obj) {
    (*env)->DeleteGlobalRef(env, gCallbackObject);
    gCallbackObject = NULL;
}

//this method is called from native codevoid nativeSayHello(char* msg) {
    int status;
    JNIEnv *env;
    int isAttached = 0;

    if (!gCallbackObject) return;

    if ((status = (*gJavaVM)->GetEnv(gJavaVM, (void**)&env, JNI_VERSION_1_6)) < 0) {
        if ((status = (*gJavaVM)->AttachCurrentThread(gJavaVM, &env, NULL)) < 0) {
            return;
        }
        isAttached = 1;
    }

    jclass cls = (*env)->GetObjectClass(env, gCallbackObject);
    if (!cls) {
        if (isAttached) (*gJavaVM)->DetachCurrentThread(gJavaVM);
        return;
    }

    jmethodID method = (*env)->GetMethodID(env, cls, "SayHello", "(Ljava/lang/String;)V");
    if (!method) {
        if (isAttached) (*gJavaVM)->DetachCurrentThread(gJavaVM);
        return;
    }

    jstring string = (*mEnv)->NewStringUTF(mEnv, msg);
    (*env)->CallVoidMethod(env, gCallbackObject, method, string);

    if (isAttached) (*gJavaVM)->DetachCurrentThread(gJavaVM);
}

This code snippet is not tested. To prevent memory leak, don't forget to call nativeRelease() method in your Java code when the reference to the object will not needed any more.

See The Java Native Interface documentation for more details.

Post a Comment for "How To Call Java Function From C"