Using java.util.Arrays.asList
will create an array with a single element being the JS array. JS arrays aren't Java arrays...
Instead of java.util.Arrays.asList
, use Java.to(jsArray, Java.type('java.util.List'))
. The resulting list will be backed by the JS array, so changes to one will be reflected by the other. You can also convert the JS array to a Java array with just Java.to(jsArray)
(which is equivalent to Java.to(jsArray, Java.type('java.lang.Object[]')
. So if you want to go through the array conversion step, you can use java.util.Arrays.asList(Java.to(jsArray));
.
In general, Nashorn will convert JS arrays to Java arrays automatically in most cases where the Java method signature specifies an array parameter, so you don't have to use Java.to()
explicitly a lot. Arrays.asList(T...)
is unfortunately a vararg method, so there's some ambiguity as to how to handle the argument.