Is it ok to save JavaVM * and JNIEnv * as a global

2019-02-19 11:06发布

问题:

JavaVM *javaVM = NULL; JNIEnv *jniEnv = NULL;

( createJavaVM )( &javaVM, ( void * )&jniEnv, &javaVMInitArgs )

where javaVM * and jniEnv * are global variables

回答1:

Is it ok to save JavaVM * as a global variable and use it elsewhere?

Yes.

Is it ok to save JNIEnv * as a global variable and use it elsewhere?

No. That represents a context for a specific JNI method invocation. It is dependent on the current thread, the arguments to the current method, the local references that have been created by the current method, all sorts of things, and it becomes invalid once that JNI method returns.