I see New Java Array of Strings in Nashorn, and find the solution.
But I just wonder why my origin code throws exception:
var JStringArray = Java.type("java.lang.String[]");
var validExtensions = new JStringArray(".java", ".class", ".jar", ".xml");
The exception is
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
To the JStringArray constructor, you can pass only number of elements of the array.
var JStringArray = Java.type("java.lang.String[]");
var validExtensions = new JStringArray(4);
validExtensions[0] = ".java";
...
All Java array type objects are constructors that accept single argument that is the "length" of the array.