10

I have byte array in java class , and i want to pass that byte array to JNI C class, I am not able to access that array in JNI C, please help.

1 Answer 1

20

you need to declare the JNI function that receives the array like this (in Java):

private native void sendData(byte[] data);

you call the function like any other function:

sendData(buffer);

and then in your C code implement the function like this:

JNIEXPORT void JNICALL Java_com_packageXXX_yourClass_sendData( JNIEnv* env, jobject thiz, jbyteArray data);

read the array:

byte * cData = env->GetByteArrayElements(data, &isCopy);

and release:

env->ReleaseByteArrayElements(data, cData, JNI_ABORT);

the above code is C++. To make it work for C you need to pass the jni environement (env) as the first parameter of the function you are calling, like this:

(*env)->GetByteArrayElements(env,...)

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

8 Comments

Thanks for great answer, But i have one problem, Method 'GetByteArrayElements' could not be resolved,,, how to remove this error???? its always shown to me when i try to use env methods??
env->ReleaseByteArrayElements(data, cData, JNI_ABORT); also could not resolved
if you are using plain C then you need (*env)->GetByteArrayElements(env, array, isCopy). Check out this link: docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/…
i get the reference of this method in jni.h , but although "GetByteArrayElements" error is showing
right now I have this in my code and it works perfectly: short * cData = (short*)env->GetShortArrayElements(data, &isCopy); Looking at the functions listed in the link above, GetByteArrayElements() is also available. Are you sure that the array passed to the function is a jbyteArray? Are you writing C or C++ code?
|

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.