Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <a href="http://download.java.net/jdk8/docs/technotes/guides/scripting/prog_guide/javascript.html#A1147390" rel="nofollow">doc</a> says ""Every JavaScript object implements the java.util.Map interface". But this sample test program shows thats not the case.</p> <pre><code>public final class NashornTestMap { public static void main(String args[]) throws Exception{ ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine nashorn = factory.getEngineByName("nashorn"); nashorn.eval("" +"load(\"nashorn:mozilla_compat.js\");" + "importClass(Packages.NashornTestMap);" + "var map={};" + "map[\"Key\"]=String(\"Val\"); " + "var test = new NashornTestMap();" + "test.test(map);" + ""); } public void test(Map&lt;String, String&gt; obj){ System.out.println(obj); } } </code></pre> <p><p> The above code give exception "Exception in thread "main" java.lang.ClassCastException: Cannot cast jdk.nashorn.internal.scripts.JO4 to java.util.Map". This <a href="http://marc.info/?l=openjdk-nashorn-dev&amp;m=138016915730891" rel="nofollow">link</a> confirms this.</p> <p>However you can use Map inside your script and call the java objects directly, like this.</p> <pre><code>public final class NashornTestMap { public static void main(String args[]) throws Exception{ ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine nashorn = factory.getEngineByName("nashorn"); nashorn.eval("" +"load(\"nashorn:mozilla_compat.js\");" + "importClass(Packages.NashornTestMap);" + "var HashMap = Java.type(\"java.util.HashMap\");" + "var map = new HashMap();" + "map.put(0, \"value1\");" + "var test = new NashornTestMap();" + "test.test(map);" + ""); } public void test(Map&lt;String, String&gt; obj){ System.out.println(obj); } } </code></pre> <p>Returns "{0=value1}"</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload