How to create an ArrayList and add element with Rust JNI?

Hello,
Is it possible to create an collection ArrayList and append object?

I tried to do smth like a:

let cls_array_list = jenv.find_class("java/util/ArrayList").unwrap();
let items_list = jenv.new_object(cls_array_list, "()V", &[]).unwrap();

then add item

let cls_item = jenv.find_class("Item").unwrap();
let obj_item = jenv.new_object(cls_item, "()V", &[]).unwrap();

jenv.call_method(
    &items_list,
    "add",
    "(Ljava/lang/Object;)V",
    &[JValue::Object(&item1)],
)
.unwrap();

But got exception:

Exception in thread "Thread-0" java.lang.NoSuchMethodError: Ljava/util/ArrayList;.add(Ljava/lang/Object;)V

Any ideas how can I create collection of Items?
Thanks!

If my java is not completely rusty (haha), V means void but add returns boolean.

I've just tried to change signature to "(Ljava/lang/Object;)B",
but got almost the same exception:

Exception in thread "Thread-0" java.lang.NoSuchMethodError: Ljava/util/ArrayList;.add(Ljava/lang/Object;)B

You should look at the docs. Booleans are Z

2 Likes
2 Likes

thank you!