title: Sharing a JavaVM* across threads in Android NDK
body: <p>I want to call Java class methods from a cpp file that receives call backs from another executable.</p>
  
  <p>To achieve this, I have retrieved a JavaVM pointer using the <strong>android::AndroidRuntime::getJavaVM()</strong> method in the .cpp file that directly receives JNI method calls. I am sharing this JavaVM pointer via the constructor to the eventual .cpp file where I call required Java methods as follows:</p>
  
  <pre><code>/* All the required objects(JNIEnv*,jclass,jmethodID,etc) are appropriately declared. */
  **JNIEnv* env;
  jvm-&gt;AttachCurrentThread(&amp;env, NULL);
  clazz = env-&gt;FindClass("com/skype/ref/NativeCodeCaller");
  readFromAudioRecord = env-&gt;GetStaticMethodID(clazz, "readFromAudioRecord", "([B)I");
  writeToAudioTrack = env-&gt;GetStaticMethodID(clazz, "writeToAudioTrack", "([B)I");** 
  </code></pre>
  
  <p>However, I get a  <strong>SIGSEGV</strong> fault running this code.</p>
  
  <p>According to the JNI documentation this seems to be the appropriate way to obtain JNIEnv in arbitary contexts: <a href="http://java.sun.com/docs/books/jni/html/other.html#26206">http://java.sun.com/docs/books/jni/html/other.html#26206</a></p>
  
  <p>Any help in this regard will be appreciated.</p>
  
  <p>Regards,
  Neeraj</p>