How to use JNI to call JAVA method from C

I want to use JNI (Java Native Interface) to call a specific java configuration method, passing a short [] buffer to it as a parameter.

The phenomenon of the Java method is as follows:

public void setPcmLeft(short[] data) { pcm_l = data; } 

From inside my C function, how can I call this method using JNI.

Currently my code is as follows:

 void Java_com_companyName_lame_LameActivity_lameDecode(JNIEnv *env, jobject jobj) { jclass class = (*env)->GetObjectClass(env, jobj); if (class != NULL) { jmethodID setLeftDatatID = (*env)->GetMethodID(env, class, "<setPcmLeft>", "void(V)"); if (setLeftDatatID == NULL) { LOGD("(Lame) No method setLeftData"); } } } 

When I ran this, setLeftDataID will be allays NULL .

Note that the jobj parameter is a passed object that contains the setPcmLeft implementation.

+4
source share
2 answers

In the call to GetMethodID() the method name does not require angle brackets, and the signature must match the Java method.

 jmethodID setLeftDatatID = (*env)->GetMethodID(env, class, "setPcmLeft", "([S)V"); 

In general, a signature has the form ( arg-types ) ret-type encoded as indicated in the link below. The argument is short [], encoded as [S The return type is V for void.

Further information is available in Chapter 3 of the Oracle JNI Guide .

+3
source

Try the following:

  jmethodID midCallBack = (*env)->GetMethodID(env, class, "setPcmLeft", "([S)V"); 
+1
source

Source: https://habr.com/ru/post/1480580/


All Articles