Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

JNI - Attach thread to a VM in an arbitrary context?

Posts: 14
C++ Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It may seem like a simple question, but how would I attach a thread to a VM that was NOT created through native code? (That is, not created through the JNI_CreateJavaVM method).

My solution consists of a Java application that calls a native shared library, and I've found it necessary to spawn a new thread from within the native code and attach it to the VM.

I see all sorts of solutions around the net that all rely on having the JavaVM pointer already available, however the only JNI related item that I have available is possibly a JNIEnv pointer. While it may not seem like the 'best practice', I am desperately limited in what I can do differently at this time.


* I mean...come on, would it kill the JNI designers to store a pointer to the JavaVM in the environment contexts?

** I also assume that this answer isn't out there on the net anywhere, because this topic is one of the first hits on Google now........

I think that I figured it out now...

The solution that I'm going with is caching the JavaVM pointer when I catch it in the JNI_OnLoad method is invoked when loading the library. I thought that caching JNI related items was considered bad practice, but oh well. It seems to work.

That's how I solved it, in-case anyone else has a similar issue.
    Bookmark Topic Watch Topic
  • New Topic