Link failure for JNI_CreateJavaVM()

2,790 views
Skip to first unread message

B Zen

unread,
Mar 17, 2010, 5:56:31 PM3/17/10
to android-ndk
I am writing an application that creates a native thread using pthread
library. I need to then invoke a Java method from this native thread.
As per my understanding, the native thread needs to be attached to JVM
in order to invoke a Java method from native thread.

JNI_CreateJavaVM() is the method that returns the reference to JVM
which can then be used to attach a native thread to JVM. This method
is listed in "jni.h" but when I try to compile the library, it gives
me "undefined reference" error for JNI_CreateJavaVM(). I searched
through some older posts and found out that this API is part of
libdvm.so library. However, I can't find this library in either NDK or
SDK directories.

Can someone please let me know where can I find libdvm.so and how can
I add it to my JNI build?

Thanks
B

David Turner

unread,
Mar 17, 2010, 6:23:13 PM3/17/10
to andro...@googlegroups.com
I'm sorry, but this function is not exposed by the NDK. Do not try to link to libdvm.so because this library
might change names in future releases of the platform. There may be other ways to get a reference to the
VM though that use the other JNI facilities.


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
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.


B Zen

unread,
Mar 17, 2010, 6:49:07 PM3/17/10
to android-ndk
Ok. Is there any other way to get reference to the JVM? Or the better
question is -- how do I attach a native thread to the JVM so that the
native thread can invoke a callback into Java (SDK) code?

Thanks
B

On Mar 17, 3:23 pm, David Turner <di...@android.com> wrote:
> I'm sorry, but this function is not exposed by the NDK. Do not try to link
> to libdvm.so because this library
> might change names in future releases of the platform. There may be other
> ways to get a reference to the
> VM though that use the other JNI facilities.
>

> On Wed, Mar 17, 2010 at 2:56 PM, B Zen <biren...@gmail.com> wrote:
> > I am writing an application that creates a native thread using pthread
> > library. I need to then invoke a Java method from this native thread.
> > As per my understanding, the native thread needs to be attached to JVM
> > in order to invoke a Java method from native thread.
>
> > JNI_CreateJavaVM() is the method that returns the reference to JVM
> > which can then be used to attach a native thread to JVM. This method
> > is listed in "jni.h" but when I try to compile the library, it gives
> > me "undefined reference" error for JNI_CreateJavaVM(). I searched
> > through some older posts and found out that this API is part of
> > libdvm.so library. However, I can't find this library in either NDK or
> > SDK directories.
>
> > Can someone please let me know where can I find libdvm.so and how can
> > I add it to my JNI build?
>
> > Thanks
> > B
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "android-ndk" group.
> > To post to this group, send email to andro...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>

David Turner

unread,
Mar 17, 2010, 7:17:07 PM3/17/10
to andro...@googlegroups.com
Just call GetJavaVM() from inside a JNI function called from your Java code.

To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

B Zen

unread,
Mar 17, 2010, 9:05:22 PM3/17/10
to android-ndk
Thanks. I can call GetJavaVM() from inside a JNI function called from
my java code to get pointer to JVM. Now, in my newly created native
thread, I invoke AttachCurrentThread(), which completes successfully.
But when I invoke FindClass() method in the native thread, it fails.
The same API works fine if I invoke it from the JNI function that is
called from java code.
What could be the cause of this failure?

Appreciate your help.
B

On Mar 17, 4:17 pm, David Turner <di...@android.com> wrote:
> Just call GetJavaVM() from inside a JNI function called from your Java code.
>

> > <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Buns...@googlegroups.com>

fadden

unread,
Mar 18, 2010, 4:10:18 PM3/18/10
to android-ndk
On Mar 17, 6:05 pm, B Zen <biren...@gmail.com> wrote:
> Thanks. I can call GetJavaVM() from inside a JNI function called from
> my java code to get pointer to JVM. Now, in my newly created native
> thread, I invoke AttachCurrentThread(), which completes successfully.
> But when I invoke FindClass() method in the native thread, it fails.
> The same API works fine if I invoke it from the JNI function that is
> called from java code.
> What could be the cause of this failure?

FindClass needs to know which class loader to use. Since there's no
Object associated with the call to FindClass, it loads classes using
the class loader associated with the Java method that called the
native method. Since your thread was attached to the VM from native
code, there is no caller. FindClass falls back on the "system" class
loader. This works out fine if you're interested in a core or
framework class, but it won't find classes associated with your
application.

The easiest way around this is to call a "find and cache the classes
I'm interested in" function from your application during
initialization. You can hold on to the results long-term if you use
NewGlobalRef to get a GC-friendly reference.

See also:
http://android.git.kernel.org/?p=platform/dalvik.git;a=blob_plain;f=docs/jni-tips.html;hb=HEAD

Reply all
Reply to author
Forward
0 new messages