Nashorn TypeError: Cannot call undefined in <eval>

时光怂恿深爱的人放手 提交于 2019-12-25 03:35:02

问题


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:


The traceback contains the Nashorn eval method, which means it is encountering an unhandled error while running the embedded 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.

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:

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.



来源:https://stackoverflow.com/questions/30156214/nashorn-typeerror-cannot-call-undefined-in-eval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!