Rust and JNI: unable to call method with Double parameter

Hello everyone,
I'm trying to call java method with 1 double parameters, but got signature exception:

java method:

public void setPrice(Double value)
    {
        this.price = value;
    }

rust side:

let val = JValue::Double(65000.00);
        jenv.call_method(&item1, "setPrice", "(Ljava/lang/Double;)V", &[val])
            .unwrap();

exception:

called `Result::unwrap()` on an `Err` value: InvalidArgList(TypeSignature { args: [Object("java/lang/Double")], ret: Primitive(Void) })

any ideas what I'm doing wrong?

thanks!

JValue::Double is a primitive double, while java.lang.Double is a class wrapping a double. Java normally automatically introduces conversions between the two, but it's just syntactic sugar. I believe this should work, but I haven't tested it.

let val = jenv.new_object("java/lang/Double", "(D)V", &[JValue::Double(65000.0)]);
jenv.call_method(&item1, "setPrice", "(Ljava/lang/Double;)V", &[val]).unwrap();
1 Like

If you control the Java code as well as the Rust code, the you should change the method to take a primitive double, as that is simpler and more efficient:

public void setPrice(double value)
1 Like

Many thanks to you @kpreid , @SkiFire13 !