Ben
Ben

Reputation: 193

Nashorn - How to get a JS Array in Java?

I have this JS function:

function newJSArray() {return []}

That I am trying to return in Java using the following method:

public static NativeArray newArray() throws Exception {
    return (NativeArray)invocable.invokeFunction("newJSArray");
}

But it's throwing an exception when I try to invoke this function:

Exception in thread "main" java.lang.NoSuchMethodError: ....JavaScript.newArray()Ljdk/nashorn/api/scripting/JSObject;
    at ....

I want to be given a JS array specifically so I can call jsArr1.concat(jsArr2).

Upvotes: 3

Views: 1047

Answers (2)

A. Sundararajan
A. Sundararajan

Reputation: 4405

Yes, all API methods return JavaScript objects as instances of jdk.nashorn.api.scripting.JSObject. You can access members of that JS array by JSObject.getSlot method.

Upvotes: 2

Ben
Ben

Reputation: 193

Found out that the object returned was a JSObject.

Upvotes: 0

Related Questions