Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it worked Nashorn JS object to java.util.Map?

Tags:

java-8

nashorn

I have java method

void someMethod(String str, Map map) {
    ...
}

From JS call this method

var map = new Object()
map.key1 = "val1"
...someMethod(str, map)

Exception:

java.lang.NoSuchMethodException: None of the fixed arity signatures
[(java.lang.String, java.util.Map)] of method org.prjctor.shell.Bash.eval
match the argument types [java.lang.String, jdk.nashorn.internal.scripts.JO]

But in Nashorn docs "Mapping of Data Types Between Java and JavaScript" said "Every JavaScript object is also a java.util.Map so APIs receiving maps will receive them directly".

What am I doing wrong?

like image 640
foror Avatar asked Dec 11 '13 07:12

foror


People also ask

What is Nashorn in Java 8?

Nashorn: Nashorn is a JavaScript engine which is introduced in JDK 8. With the help of Nashorn, we can execute JavaScript code at Java Virtual Machine. Nashorn is introduced in JDK 8 to replace existing JavaScript engine i.e. Rhino.

Does Nashorn support ES6?

While the Nashorn team didn't have time to deliver the complete ES6 feature set for the JDK 9 release, more ES6 features will follow in future updates. To activate ES6 support, use --language=es6 on the command line.

What is JDK Nashorn?

The Nashorn engine is included in the Java SE Development Kit (JDK). You can invoke Nashorn from a Java application using the Java Scripting API to interpret embedded scripts, or you can pass the script to the jjs or jrunscript tool. Note: Nashorn is the only JavaScript engine included in the JDK.

How do Java maps work?

More specifically, a Java Map can store pairs of keys and values. Each key is linked to a specific value. Once stored in a Map , you can later look up the value using just the key. The Java Map interface is not a subtype of the Collection interface.


1 Answers

Agree with the previous answers that you cannot do this as the docs have implied. However you could create and pass a map as follows

..
var HashMap = Java.type('java.util.HashMap');
var map = new HashMap();
map.put('1', 'val1');
...someMethod(str, map)
like image 161
Joseph Rajeev Motha Avatar answered Sep 21 '22 10:09

Joseph Rajeev Motha