hkropp
hkropp

Reputation: 563

How can I put a Class into Scope of a JJS ScriptEngine in Scala

I fail to get a Class into scope of the ScriptEngine while running nashorn in scala.

Here is what I try to do:

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)

Does anyone know how to initiate MyClass from JavaScript?

I get the following Exception:

javax.script.ScriptException: ReferenceError: "MyClass" is not defined in <eval> at line number 1`

Upvotes: 0

Views: 317

Answers (1)

aepurniet
aepurniet

Reputation: 1727

i would assume its would be the same as trying to access java classes, its worth a try

http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/javascript.html#A1147187 suggests

var MyClass = Java.type("MyClass");
var inst = new MyClass("myId", "myName");

Upvotes: 2

Related Questions