我在scala中运行Class
时未能ScriptEngine
进入nashorn
的范围。
这是我尝试做的事情:
import javax.script.{ScriptEngine, ScriptEngineManager}
case class MyClass(id: Option[String], name: Option[String])
val manager: ScriptEngineManager = new ScriptEngineManager
val engine: ScriptEngine = manager.getEngineByName("nashorn")
var simpleScript: String =
"""
var myClass = new MyClass('myId', 'myName'); print(myClass.id);
"""`
engine.eval(simpleScript)
是否有人知道如何从MyClass
启动JavaScript
?
我得到以下例外:
javax.script.ScriptException: ReferenceError: "MyClass" is not defined in <eval> at line number 1`
答案 0 :(得分:2)
我认为它与尝试访问java类是一样的,值得一试
http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/javascript.html#A1147187建议
var MyClass = Java.type("MyClass");
var inst = new MyClass("myId", "myName");