Search code examples
javac++java-native-interfacebytebuffernosuchmethoderror

JNI ByteBuffer put bytes


JNIEXPORT jboolean JNICALL Java_directshowcamera_dsInterface_grab_1frame_1stream(JNIEnv *env, jobject obj, jint streamid, jobject barray)
{
    jclass bbclass = env->FindClass( "java/nio/ByteBuffer" );
    jmethodID putMethod = env->GetMethodID(bbclass, "put", "(I, B)Ljava/nio/ByteBuffer");
    unsigned char *buffer = stream_buffer( streamid );

    if( !stream_image_ready( streamid ) ) return (jboolean)0;

    for(int i=0; i < stream_device_size( streamid ); i++ ) {
        env->CallByteMethod( barray, putMethod, i, (jbyte)buffer[i] );
    }

    return (jboolean)1;
}

So, I have a byte buffer, and in Java I've allocated the appropriate size, and noticed it's possible to to ByteBuffer.put( index, byte ), so I tried to get the method, but when I do in Java, I get the following runtime exception:

java.lang.NoSuchMethodError: put

Any ideas what I've done wrong? I don't know JNI that well, and have mostly been re-working examples I've found off the web.


Solution

  • The method signature (I, B)Ljava/nio/ByteBuffer is wrong. There is no , between arguments in method signatures and classes are L<class>; (you forgot the ;).

    So the correct signature is: (IB)Ljava/nio/ByteBuffer;

    Then the code should work.