How to obtain JavaVM pointer in jni cpp code

5,173 views
Skip to first unread message

Elvis Dowson

unread,
Jul 11, 2009, 9:35:40 AM7/11/09
to android-ndk
Hi,
Would anyone have a code snippet on how I can obtain a pointer
to the android JavaVM, in my jni cpp code?

Best regards,

Elvis Dowson

Anders Hasselqvist

unread,
Jul 11, 2009, 9:54:23 AM7/11/09
to andro...@googlegroups.com
Hi Elvis,

You get it in the JNI_OnLoad() function that your native library
provides. You then cache it in your library in some way so you can
access it where you need.

(Look here http://java.sun.com/docs/books/jni/html/other.html
and here http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/invocation.html)

/Anders

2009/7/11 Elvis Dowson <elvis....@gmail.com>:

Elvis Dowson

unread,
Jul 12, 2009, 10:19:28 AM7/12/09
to android-ndk
Hi,
Here is a working code snippet. Since all android processes have
only 1 jvm, I've just limited to the max number of returned jvms to 1.


// Acquire a pointer to the current JavaVM
jsize jvmBufferLength = 1; // At most vmBufLength number of entries
will be written for the list of returned JavaVMs
jsize jvmTotalNumberFound = 0; // The total number of JavaVMs found
JavaVM jvmBuffer[jvmBufferLength]; // Array of JavaVMs
JavaVM * pjvmBuffer = jvmBuffer; // Pointer to array of JavaVMs
jint result = JNI_GetCreatedJavaVMs( &pjvmBuffer, jvmBufferLength,
&jvmTotalNumberFound); // Get all created JavaVMs

LOGI("Found %d JavaVM instances\n", jvmTotalNumberFound);

# Android.mk for the jni project.
# All of the shared libraries we link against.
LOCAL_SHARED_LIBRARIES := \
libandroid_runtime \
libnativehelper \
libdvm \
libcutils \
libutils


Best regards,

Elvis

fadden

unread,
Jul 13, 2009, 1:55:58 PM7/13/09
to android-ndk
On Jul 12, 7:19 am, Elvis Dowson <elvis.dow...@gmail.com> wrote:
> jint result     = JNI_GetCreatedJavaVMs( &pjvmBuffer, jvmBufferLength,
> &jvmTotalNumberFound); // Get all created JavaVMs

That looks correct.

It's generally hard to go wrong caching a JavaVM, so long as you don't
keep using it after the VM goes away; since the VM doesn't go away
until the Android app is killed, you're safe. Where people often go
wrong is caching a JNIEnv and mistakenly using it from multiple
threads. (CheckJNI will catch that.)

spietari

unread,
Jul 14, 2009, 5:20:41 AM7/14/09
to android-ndk

Hi, on Windows & cygwin this fails

make: *** No rule to make target 'out/apps/test/android-1.5-arm/
libandroid_runtime.so', needed by 'out/apps/test/android-1.5-arm/
libtest.so'. Stop.

How can I fix this please?

Seppo

David Turner

unread,
Jul 14, 2009, 12:09:34 PM7/14/09
to andro...@googlegroups.com
The error message indicates that your libtest.so needs libandroid_runtime.so,
but you don't have this library in your modules list (see APP_MODULES in Application.mk).

If this is the libandroid_runtime.so that comes with a normal Android system image,
linking will not work anyway since this is not distributed with the NDK.
Reply all
Reply to author
Forward
0 new messages