Discussion:
Link failure for JNI_CreateJavaVM()
B Zen
2010-03-17 21:56:31 UTC
Permalink
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 android-***@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.
David Turner
2010-03-17 22:23:13 UTC
Permalink
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.
Post by B Zen
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 unsubscribe from this group, send email to
.
For more options, visit this group at
http://groups.google.com/group/android-ndk?hl=en.
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to android-***@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
2010-03-17 22:49:07 UTC
Permalink
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
Post by David Turner
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.
Post by B Zen
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 unsubscribe from this group, send email to
.
For more options, visit this group at
http://groups.google.com/group/android-ndk?hl=en.
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to android-***@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.
David Turner
2010-03-17 23:17:07 UTC
Permalink
Just call GetJavaVM() from inside a JNI function called from your Java code.
Post by B Zen
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
Post by David Turner
I'm sorry, but this function is not exposed by the NDK. Do not try to
link
Post by David Turner
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.
Post by B Zen
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
Post by David Turner
Post by B Zen
"android-ndk" group.
To unsubscribe from this group, send email to
.
For more options, visit this group at
http://groups.google.com/group/android-ndk?hl=en.
--
You received this message because you are subscribed to the Google Groups
"android-ndk" group.
To unsubscribe from this group, send email to
.
For more options, visit this group at
http://groups.google.com/group/android-ndk?hl=en.
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to android-***@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
2010-03-18 01:05:22 UTC
Permalink
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
Post by David Turner
Just call GetJavaVM() from inside a JNI function called from your Java code.
Post by B Zen
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
Post by David Turner
I'm sorry, but this function is not exposed by the NDK. Do not try to
link
Post by David Turner
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.
Post by B Zen
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
Post by David Turner
Post by B Zen
"android-ndk" group.
To unsubscribe from this group, send email to
.
For more options, visit this group at
http://groups.google.com/group/android-ndk?hl=en.
--
You received this message because you are subscribed to the Google Groups
"android-ndk" group.
To unsubscribe from this group, send email to
.
For more options, visit this group at
http://groups.google.com/group/android-ndk?hl=en.
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to android-***@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.
fadden
2010-03-18 20:10:18 UTC
Permalink
Post by B Zen
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
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to android-***@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.
Loading...