简体   繁体   中英

ScriptException converting json to map Java 8 Nashorn

As with the recent Java update 8u60 java has supported json to map converstion using Nashorn Script Engine, I have been trying to checkout same using the sample code provided here . However, its giving me ScriptException with the below stacktrace. I'm using Java 8u65 to run the given code.

Stacktrace:

Exception in thread "main" javax.script.ScriptException: TypeError: Cannot call undefined in at line number 1 at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:455) at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:439) at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:401) at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:397) at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:152) at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264) at JSONTest.main(JSONTest.java:8) Caused by: :1 TypeError: Cannot call undefined at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57) at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:213)

Will you please check your JDK version? It should print something like:

$ java -version
java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)

I compiled and run JSONTest.java from Nashorn wiki page with both JDK 8u60 and 8u65. It worked as expected:

$ java JSONTest
343
2
4
5

I tested on Windows 7, 64 bit version with Cygwin shell (but that should not produce any difference in behavior)

确保包含jdk的jar文件在您的类路径中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM