如果有什麼,我能做些什麼來阻止Nashorn始終拋出ArrayIndexOutOfBoundsException?Java 8 Nashorn ArrayIndexOutOfBoundsException
這個看似微不足道的,正確的(有沒有錯誤本文中)的代碼片段導致犀牛的例外:
文件test.js:
var test;
(function (test) {
(function (renderer) {
var posts = {
0: null,
1: null,
2: null,
3: null // comment out this line --> no exception thrown
};
})(test.renderer = {}); // comment out 'test.renderer = ' --> no exception thrown
})(test = {});
然後運行該腳本,像這樣:
/usr/lib/jvm/java-8-oracle/bin/jjs test.js
結果:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at jdk.nashorn.internal.runtime.AccessorProperty.initGetterSetter(AccessorProperty.java:271)
at jdk.nashorn.internal.runtime.AccessorProperty.<init>(AccessorProperty.java:250)
at jdk.nashorn.internal.runtime.SetMethodCreator.createNewFieldSetter(SetMethodCreator.java:166)
at jdk.nashorn.internal.runtime.SetMethodCreator.createNewPropertySetter(SetMethodCreator.java:156)
at jdk.nashorn.internal.runtime.SetMethodCreator.createSetMethod(SetMethodCreator.java:122)
at jdk.nashorn.internal.runtime.SetMethodCreator.createGuardedInvocation(SetMethodCreator.java:78)
at jdk.nashorn.internal.runtime.ScriptObject.findSetMethod(ScriptObject.java:1911)
at jdk.nashorn.internal.runtime.ScriptObject.lookup(ScriptObject.java:1632)
at jdk.nashorn.internal.runtime.linker.NashornLinker.getGuardedInvocation(NashornLinker.java:96)
at jdk.internal.dynalink.support.CompositeTypeBasedGuardingDynamicLinker.getGuardedInvocation(CompositeTypeBasedGuardingDynamicLinker.java:176)
at jdk.internal.dynalink.support.CompositeGuardingDynamicLinker.getGuardedInvocation(CompositeGuardingDynamicLinker.java:124)
at jdk.internal.dynalink.support.LinkerServicesImpl.getGuardedInvocation(LinkerServicesImpl.java:144)
at jdk.internal.dynalink.DynamicLinker.relink(DynamicLinker.java:232)
at jdk.nashorn.internal.scripts.Script$test.L:2(test.js:3)
at jdk.nashorn.internal.scripts.Script$test.runScript(test.js:2)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:535)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:209)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
at jdk.nashorn.tools.Shell.apply(Shell.java:383)
at jdk.nashorn.tools.Shell.runScripts(Shell.java:312)
at jdk.nashorn.tools.Shell.run(Shell.java:168)
at jdk.nashorn.tools.Shell.main(Shell.java:132)
at jdk.nashorn.tools.Shell.main(Shell.java:111)
當我通過腳本引擎從JVM中運行文件時,也會發生這種情況。
背景:我試圖在Nashorn中運行TypeScript生成的代碼。上面的代碼片段是簡化的TypeScript編譯器輸出。
操作系統:Linux Mint 17,64位。 Java版本:
$ java -version
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)
您可以發佈java -version的輸出嗎?我注意到Nashorn正在改變JDK 1.8的各種小版本,所以它可能會有所作爲。 – 2014-10-04 11:33:41
@ DavidP.Caldwell是的,我忘了,完成了(附加到問題)。 – KajMagnus 2014-10-05 05:10:00
嗨@Jarrod Robertson,實際上這不是http://stackoverflow.com/questions/32568261/how-to-avoid-java-lang-arrayindexoutofboundsexception的重複。這個問題是關於Nashorn *總是拋出'ArrayIndexOutOfBoundsException',即使是*正確的*代碼 - 原因是Nashorn中的錯誤。 – KajMagnus 2016-03-04 03:21:59