As part of synchronous calls using the JNI from Java to C ++, the "environment" has already been configured by the JVM, however, moving in the other direction from an arbitrary C ++ stream, it may not have been
Therefore, you need to follow these steps
- Get JVM Environment Context Using
GetEnv - if necessary, associate the context with
AttachCurrentThread - call the method as usual using
CallVoidMethod - detach using
DetachCurrentThread
Full example. Note. I wrote about this in the past in more detail on the blog.
void callback(int val) { JNIEnv * g_env; // double check it all ok int getEnvStat = g_vm->GetEnv((void **)&g_env, JNI_VERSION_1_6); if (getEnvStat == JNI_EDETACHED) { std::cout << "GetEnv: not attached" << std::endl; if (g_vm->AttachCurrentThread((void **) &g_env, NULL) != 0) { std::cout << "Failed to attach" << std::endl; } } else if (getEnvStat == JNI_OK) { // } else if (getEnvStat == JNI_EVERSION) { std::cout << "GetEnv: version not supported" << std::endl; } g_env->CallVoidMethod(g_obj, g_mid, val); if (g_env->ExceptionCheck()) { g_env->ExceptionDescribe(); } g_vm->DetachCurrentThread(); }
Adam Oct. 15 '12 at 17:47 2012-10-15 17:47
source share