Nashorn TypeError:无法在 <eval> 中调用 undefined [英] Nashorn TypeError: Cannot call undefined in <eval>

查看:62
本文介绍了Nashorn TypeError:无法在 <eval> 中调用 undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

错误描述:

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 时遇到未处理的错误.

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

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

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 类名称 调用Java.type,然后调用返回的函数从JavaScript 实例化一个类.尝试在较小的脚本中创建一个圆,如下所示:

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();
// ...

并从那里建立起来;请务必将 mypackage 替换为此代码的实际包名称.

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

这篇关于Nashorn TypeError:无法在 &lt;eval&gt; 中调用 undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆