gpt4 book ai didi

java - Nashorn 类型错误 : Cannot call undefined in

转载 作者:行者123 更新时间:2023-12-02 04:41:15 25 4
gpt4 key购买 nike

运行下面的代码时,出现错误。我不知道是什么导致了这个错误。

ScriptEngine engine = engineManager.getEngineByName("nashorn");  
String str = "var shape_objects = [ Java.Type(\"new Triangle()\"), Java.Type(\"new Circle()\"), Java.Type(\"new Rectangle()\"), Java.Type(\"new Shape()\")];"+
"var colors = [\"Red\", \"Green\", \"Blue\", \"Abstract\"];"+
"var j = 0;"+
"for(var i in shape_objects) {"+
" shape_objects[i].setColor(colors[j]);"+
" j = j+1;"+
"}"+
"for(var k in shape_objects) {"+
" print(shape_objects[k].getColor());"+
"}";
engine.eval(str);



// Class definition for other Shape classes is similar
public class Circle {
private String color;
public String setColor(String color) {
this.color = new String(color);
System.out.println("Color of Circle is set to : " + this.color);
return this.color;
}
public String getColor() {
return color;
}
}

错误描述:

Exception in thread "main" javax.script.ScriptException: TypeError:
Cannot call undefined in <eval> at line number 1
at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:455)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:439)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:401)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:397)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:152)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
at nashorntest.Test.main(Test.java:40)
Caused by: <eval>:1 TypeError: Cannot call undefined
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.Undefined.lookupTypeError(Undefined.java:128)
at jdk.nashorn.internal.runtime.Undefined.lookup(Undefined.java:100)
at jdk.nashorn.internal.runtime.linker.NashornLinker.getGuardedInvocation(NashornLinker.java:102)
at jdk.nashorn.internal.runtime.linker.NashornLinker.getGuardedInvocation(NashornLinker.java:94)
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:149)
at jdk.internal.dynalink.DynamicLinker.relink(DynamicLinker.java:233)
at jdk.nashorn.internal.scripts.Script$\^eval\_.:program(<eval>:1)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:636)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)

最佳答案

回溯包含 Nashorn eval 方法,这意味着它在运行嵌入式 JavaScript 时遇到未处理的错误。

我认为它可能位于脚本的第一行:用于实例化 Java 对象的方法可能不存在于脚本中使用的名称下。

docs也就是说,使用完全限定的 Java 类名称调用Java.type,然后调用返回的函数以从 JavaScript 实例化一个类。尝试在较小的脚本中创建一个圆圈,如下所示:

var Circle = Java.type("mypackage.Circle");
var myCircle = new Circle();
// ...

并从那里开始构建;请务必将 mypackage 替换为该代码的实际包名称。

关于java - Nashorn 类型错误 : Cannot call undefined in <eval>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30156214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com