我看到了New Java Array of Strings in Nashorn,并找到了解决方案。
但我只是想知道为什么我的原始代码会抛出异常:
var JStringArray = Java.type("java.lang.String[]");
var validExtensions = new JStringArray(".java", ".class", ".jar", ".xml");
例外是
Exception in thread "main" javax.script.ScriptException: TypeError: Can not create new object with constructor [Ljava.lang.String; with the passed arguments; they do not match any of its method signatures. in ~/scriptMonkey/js/samples/FileStatistics.js at line number 9
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 Debugger.main(Debugger.java:16)
Caused by: ~/scriptMonkey/js/samples/FileStatistics.js:9 TypeError: Can not create new object with constructor [Ljava.lang.String; with the passed arguments; they do not match any of its method signatures.
at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)
at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:213)
at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:185)
at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:172)
at jdk.nashorn.internal.runtime.linker.NashornStaticClassLinker.checkNullConstructor(NashornStaticClassLinker.java:108)
at jdk.nashorn.internal.runtime.linker.NashornStaticClassLinker.getGuardedInvocation(NashornStaticClassLinker.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:154)
at jdk.internal.dynalink.DynamicLinker.relink(DynamicLinker.java:253)
at jdk.nashorn.internal.scripts.Script$1$FileStatistics.:program(~/scriptMonkey/js/samples/FileStatistics.js:9)
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.internal.runtime.Context.evaluateSource(Context.java:1222)
at jdk.nashorn.internal.runtime.Context.load(Context.java:839)
at jdk.nashorn.internal.objects.Global.load(Global.java:1545)
at jdk.nashorn.internal.scripts.Script$\^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
答案 0 :(得分:1)
对于JStringArray构造函数,您只能传递数组的元素数。
var JStringArray = Java.type("java.lang.String[]");
var validExtensions = new JStringArray(4);
validExtensions[0] = ".java";
...
所有Java数组类型对象都是接受单个参数的构造函数,该参数是&#34; length&#34;数组。