title: How to pass char array from C JNI function to Java method as byte[]
body: <p>I'm having trouble finding the right documentation for passing a char buffer from JNI method to Java method. Here's the code</p>
  
  <pre><code>jint JNICALL Java_foo_package_MyJavaClass_myNativeMethod(JNIEnv *jenv, jobject jobj)
  {
      jclass clazz = (*jenv)-&gt;GetObjectClass(jenv, jobj);
      //  MyJavaClass method:  private void addData(byte[] data)
      jmethodID mid = (*jenv)-&gt;GetMethodID(jenv, clazz, "addData", "([B)V");
      assert(mid);
  
      const char buf[] = { 0, 1, 2, 3, 42 };
      const size_t buf_len = sizeof buf;
  
      (*jenv)-&gt;CallVoidMethod(jenv, jobj, mid, buf /* obviously wrong */ );
  
      return 0;
  }
  </code></pre>
  
  <p>Is <code>CallVoidMethod</code> the right function to use here, what's the correct thing to pass to it, how to allocate it, and how (if at all) it should be freed?</p>
  
  <p>A code snippet would probably be the most compact answer, with a few words explaining how ownership of objects goes.</p>