Nashorn TypeError:无法在中调用undefined

时间:2022-12-14 21:09:28

While running below code, I am getting error. I have no idea what is causing this error.

在代码下运行时,我收到错误。我不知道是什么导致了这个错误。

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;  
        }
    }

Error description:

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)

1 个解决方案

#1


The traceback contains the Nashorn eval method, which means it is encountering an unhandled error while running the embedded JavaScript.

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

I think it's likely on the first line of the script: the methods called to instantiate your Java objects may not exist under the names used in the script.

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

The docs on this say to call Java.type with a fully qualified Java class name, and then to call the returned function to instantiate a class from JavaScript. Try creating a circle in a smaller script that looks like:

关于这个的文档说要用完全限定的Java类名调用Java.type,然后调用返回的函数来从JavaScript实例化一个类。尝试在较小的脚本中创建一个圆圈,如下所示:

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

and building up from there; be sure to replace mypackage with the actual package name for this code.

并从那里建立起来;请务必使用此代码的实际包名替换mypackage。

#1


The traceback contains the Nashorn eval method, which means it is encountering an unhandled error while running the embedded JavaScript.

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

I think it's likely on the first line of the script: the methods called to instantiate your Java objects may not exist under the names used in the script.

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

The docs on this say to call Java.type with a fully qualified Java class name, and then to call the returned function to instantiate a class from JavaScript. Try creating a circle in a smaller script that looks like:

关于这个的文档说要用完全限定的Java类名调用Java.type,然后调用返回的函数来从JavaScript实例化一个类。尝试在较小的脚本中创建一个圆圈,如下所示:

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

and building up from there; be sure to replace mypackage with the actual package name for this code.

并从那里建立起来;请务必使用此代码的实际包名替换mypackage。