在学习尚学堂视频过程中,跟着老师敲代码,但是自己的代码报错:
Exception in thread "main" javax.script.ScriptException: ReferenceError: "importPackage" is not defined in <eval> at line number 1
at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:454)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
at com.study.test.Demo01.main(Demo01.java:41)
Caused by: <eval>:1 ReferenceError: "importPackage" is not defined
at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)
at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:319)
at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:291)
at jdk.nashorn.internal.objects.Global.__noSuchProperty__(Global.java:1441)
at jdk.nashorn.internal.scripts.Script$4$\^eval\_.:program(<eval>:1)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)
... 5 more
报错的代码为:
String jsCode = "importPackage(java.util); var list = Arrays.asList([\"北京大学\",\"清华大学\"]);";
后来经过自己查资料,发现这个是JDK1.6的写法,在JDK1.8中,写法为:
String jsCode ="var list = java.util.Arrays.asList([\"清华大学\",\"北京大学\"]);";
看来学习也需要,与时俱进啊。