Get an undefined reference to `JNI_GetCreatedJavaVMs'

906 views
Skip to first unread message

Bibu

unread,
Apr 26, 2012, 5:22:43 AM4/26/12
to andro...@googlegroups.com
I'm trying to compile with ndk-build my C code, but I get this error :

> undefined reference to `JNI_GetCreatedJavaVMs'

After a quick search I found this question : http://stackoverflow.com/questions/8145985/android-ndk-cant-find-jni-getcreatedjavavms-after-update-from-r6-to-r7 but I don't understand his solution.

I'm a beginner with JNI, so don't be so hard with me please.

Bibu

unread,
May 3, 2012, 10:23:06 AM5/3/12
to andro...@googlegroups.com
 I've finaly understand the solution given on stackoverflow by doing something like this :

JNIEnv *env;
jclass myClass;
jclass myInstance;

JNIEXPORT  jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
    (*vm)->AttachCurrentThread(vm, (void*)&env, NULL);
    myClass = (*env)->FindClass(env, "com/main/MyClass");
    myInstance = (*env)->FindClass(env, "com/main/MyInstance");
    return JNI_VERSION_1_6;
}

One other note about JNI_OnLoad: any FindClass calls you make from there will happen in the context of the class loader that was used to load the shared library. Normally FindClass uses the loader associated with the method at the top of the interpreted stack, or if there isn't one (because the thread was just attached) it uses the "system" class loader. This makes JNI_OnLoad a convenient place to look up and cache class object references. 
 
 If you call FindClass from this thread, the JavaVM will start in the "system" class loader instead of the one associated with your application, so attempts to find app-specific classes will fail.
There are a few ways to work around this:
  • Do your FindClass lookups once, in JNI_OnLoad, and cache the class references for later use. Any FindClass calls made as part of executing JNI_OnLoad will use the class loader associated with the function that calledSystem.loadLibrary (this is a special rule, provided to make library initialization more convenient). If your app code is loading the library, FindClass will use the correct class loader.
 That's why I use FindClass on JNI_OnLoad() method.

But now, my application crash after the second call of CallObjectMethod ??
On the first call everything is correct, but on the second, the application crash, the difference between the two calls are the methodName, so I check the result of GetMethodID but no error ...

What am I missing ??

David Turner

unread,
May 3, 2012, 10:46:46 AM5/3/12
to andro...@googlegroups.com
FindClass returns a local reference that only works in the current thread. So it may be that using the reference in a different thread will crash / not work properly.
You should promote these to global references before storing them as global variables.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/android-ndk/-/JLsfoKKfvNEJ.

To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

Bibu

unread,
May 4, 2012, 3:36:48 AM5/4/12
to andro...@googlegroups.com
Thanks Digit, you're totaly right, I've enabled CHECK JNI :  http://android-developers.blogspot.fr/2011/07/debugging-android-jni-with-checkjni.html 

And I got that :
05-04 09:31:15.648: E/dalvikvm(5698): JNI ERROR: non-VM thread making JNI calls
 
I will try your solution, but I actually create an application under Android 3.2, not 4.0(ICS), but will see if it works, I will get you advise from the result.
Reply all
Reply to author
Forward
0 new messages