Javascript object to Map

A. Sundararajan sundararajan.athijegannathan at oracle.com
Wed Sep 25 21:18:27 PDT 2013


Hi.

Yes, that is expected. A nashorn script object is not a java.util.Map. 
You've to walk through each property and put prop-name-vs-value into the 
Map. Something like:

var obj  = { key1: value1, key2: value2 };

for (i in obj) {
     map.put(i, obj[i]);
}

Hope this helps,
-Sundar

On Thursday 26 September 2013 02:05 AM, Júnior wrote:
> Hi,
>
> Reading the point 3.10 from this link:
> http://download.java.net/jdk8/docs/technotes/guides/scripting/prog_guide/javascript.html#A1147390
>
> If I didn't misunderstand that, if I have a javascript object I can pass it
> to a java method that accepts a java.util.Map.
>
> So, this code is supposed to work:
>
> [code]
>
> var HashMap = Java.type("java.util.HashMap");
>
> var map = new HashMap();
>
> map.putAll({key:"value",key2:"value2"});
>
> [/code]
>
> But when executing this using the jjs I got this error:
>
>
> *"Exception in thread "main" java.lang.ClassCastException: Cannot cast
> jdk.nashorn.internal.scripts.JO to java.util.Map"*
>
> Is this the expected behavior?
>
> Thanks,



More information about the nashorn-dev mailing list