大家好,我是烤鸭:
今天使用 javax.script.ScriptEngine 遇到一个奇怪的问题,无法识别js方法。
1. 报错内容:
javax.script.ScriptException: ReferenceError: "a" is not defined in <eval> at line number 1057at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470) ~[nashorn.jar:na]at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:392) ~[nashorn.jar:na]at jdk.nashorn.api.scripting.NashornScriptEngine.invokeFunction(NashornScriptEngine.java:190) ~[nashorn.jar:na]at com.mys.my.wechat.utils.aes.JSSecret.getDatas(JSSecret.java:50) ~[classes!/:1.0.0-SNAPSHOT]at com.mys.my.wechat.WxMpDemoApplication.main(WxMpDemoApplication.java:26) [classes!/:1.0.0-SNAPSHOT]at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_201]at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201]at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_201]at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [wxChat.jar:1.0.0-SNAPSHOT]at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [wxChat.jar:1.0.0-SNAPSHOT]at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [wxChat.jar:1.0.0-SNAPSHOT]at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [wxChat.jar:1.0.0-SNAPSHOT]
Caused by: jdk.nashorn.internal.runtime.ECMAException: ReferenceError: "a" is not definedat jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57) ~[nashorn.jar:na]at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:319) ~[nashorn.jar:na]at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:291) ~[nashorn.jar:na]at jdk.nashorn.internal.objects.Global.__noSuchProperty__(Global.java:1442) ~[nashorn.jar:na]at jdk.nashorn.internal.scripts.Script$Recompilation$17$41307AAAA$\^eval\_.d(<eval>:1057) ~[na:na]at jdk.nashorn.internal.scripts.Script$Recompilation$16$41694A$\^eval\_.myFunc(<eval>:1069) ~[na:na]at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:639) ~[nashorn.jar:na]at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494) ~[nashorn.jar:na]at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393) ~[nashorn.jar:na]at jdk.nashorn.api.scripting.ScriptObjectMirror.callMember(ScriptObjectMirror.java:199) ~[nashorn.jar:na]at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:386) ~[nashorn.jar:na]... 11 common frames omitted
2. 问题查找
不说了, 贴几张图全明白了。下次还是仔细点吧.
服务器上的文件大小,41KB。
本地文件,42 KB.
对比后发现多了一个字符,导致无法识别方法。
总结:
其实一开始报错已经指出 1057行代码有问题,找了本地的文件没问题,没注意线上和本地文件有差异。
关于如何使用 javax.script.ScriptEngine 使用js 方法,可以参考这篇文章
https://www.cnblogs.com/jifeng/p/8901168.html