rustjava-native-interfacejnienvjniwrapper

expected `*mut jni::sys::_jobject`, found struct `jni::objects::JPrimitiveArray` error in Rust JNI call


I'm trying to call a rust library from java package and as part of that, i'm converting (serializing) an object before sending it over the wire.

How ever, I'm getting the following error.

expected `*mut jni::sys::_jobject`, found struct `jni::objects::JPrimitiveArray`

code sample:

fn serialize_response<T: Message>(
    env: &JNIEnv,
    value: T,
) -> Result<jbyteArray, ClientJniError> {
    let serialized = value.write_to_bytes()?;
    Ok(env.byte_array_from_slice(serialized.as_slice())?)
}

What am I doing wrong here?

Similar problem, while trying to deserialize the input.

fn deserialize_from_jni<T: Message>(env: &JNIEnv, buffer: &jbyteArray) -> Result<T, ClientJniError> {
    let buffer_deserialized =
        Message::parse_from_bytes(&*env.convert_byte_array(buffer)?.as_slice())?;
    Ok(buffer_deserialized)
}
Message::parse_from_bytes(&*env.convert_byte_array(buffer)?.as_slice())?;
                                ------------------ ^^^^^^ the trait `std::convert::AsRef<jni::objects::JPrimitiveArray<'_, i8>>` is not implemented for `*mut jni::sys::_jobject`
                                |
                                required by a bound introduced by this call

Solution

  • Using an older version of jni fixed the issue.