0

在 scala 中运行时,我无法Class进入范围。ScriptEnginenashorn

这是我尝试做的事情:

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`
4

1 回答 1

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");
于 2014-05-08T14:25:42.153 回答