5

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

Java method implimentation looks as follows:

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

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

My code currently looks like this:

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 run this, the setLeftDataID is allays NULL.

Note that the jobj parameter is my object being passed in that contains the setPcmLeft implementation.

2 Answers 2

4

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

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

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

More information is available in Chapter 3 of the Oracle JNI guide.

Sign up to request clarification or add additional context in comments.

2 Comments

When do we need the angle brackets? I see some code like this jmethodID m_mid = env -> GetMethodID(m_cls,"<init>","()V");
@smwikipedia - You need them in the special name <init>, which GetMethodID() supports to allow reference to constructors. "To obtain the method ID of a constructor, supply <init> as the method name and void (V) as the return type." -- from the documentation for GetMethodID() in docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/….
1

Try this:

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

4 Comments

You can use one of the CallVoidMethod*() functions.
He may have used your earlier version without "[S".
Sorry I removed that comment, I made a mistake in my code. I was missing [S. Thanks for your answer.
Ah, yes I missed the parameters and only noticed it after you posted yours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.