title: How to call getStackTrace method from java native interface (jni)
body: <p>I am trying to understand the jni with some examples. I am trying to get the java stack trace using jni and so this is what I was doing </p>
  
  <p>HelloJNI.java</p>
  
  <pre><code>package test.com.jni;
  
  public class HelloJNI {
      static {
          System.loadLibrary("hello"); // Load native library at runtime
      }
  
      private native StackTraceElement[] getStackTraceNative(Throwable throwable);
      private native void printStackTraceNative(Throwable throwable);
  
      public static void main(String[] args) {
          test();
      }
  
      public static void test() {
          new HelloJNI().printStackTraceNative(new Throwable()); //Invoke native method
          new HelloJNI().getStackTraceNative(new Throwable());
      }
  }
  </code></pre>
  
  <p>Native code (keeping the error handling out for simplicity)</p>
  
  <p>test_com_jni_HelloJNI.c</p>
  
  <pre><code>JNIEXPORT jobjectArray JNICALL Java_test_com_jni_HelloJNI_getStackTraceNative (JNIEnv * env, jobject object, jthrowable exception) {
  
      jclass exceptionClazz = (*env)-&gt;GetObjectClass(env, exception); 
  
      jmethodID getStackTraceMethod = (*env)-&gt;GetMethodID(env, exceptionClazz, "getStackTrace", "()[Ljava.lang.StackTraceElement");
  
      jobjectArray stacktraces = (*env)-&gt;CallObjectMethod(env, exception, getStackTraceMethod);
  
      return stacktraces;
  }
  
  JNIEXPORT void JNICALL Java_test_com_jni_HelloJNI_printStackTraceNative (JNIEnv * env, jobject object, jthrowable exception) {
  
      jclass exceptionClazz = (*env)-&gt;GetObjectClass(env, exception); // can't fail
  
      jmethodID printStackTraceMethod = (*env)-&gt;GetMethodID(env, exceptionClazz, "printStackTrace", "()V");
  
      (*env)-&gt;CallVoidMethod(env, exception, printStackTraceMethod);
  }
  </code></pre>
  
  <p>Now in this code native <code>printStackTraceNative</code> method works and it prints the stack trace however, <code>getStackTraceNative</code> doesn't. When I check the core dump file it says that java has thrown an exception <code>java/lang/NoSuchMethodError</code> for <code>getStackTrace</code>. I am confused because the parameter I am passing to getStackTraceNative method is of the type throwable and throwable should have the method <code>getStackTrace</code>. </p>
  
  <p>What concept I possibly be missing here, any help on this is appreciated. Thanks </p>