How to call function or module using ScriptEngine.
here is my sample code , which is compiling fine , but at runtime its throwing exception scalaVersion := "2.12.4" and sbt.version = 0.13.16, java is jdk1.8.0_131
import java.io.FileReader
import javax.script._
object DemoApp extends App {
val engine: ScriptEngine with Compilable with javax.script.Invocable = new ScriptEngineManager()
.getEngineByName("scala")
.asInstanceOf[ScriptEngine with javax.script.Invocable with Compilable]
val reader = new FileReader("src/main/scala/Demo.sc")
engine.compile(reader).eval()
val result = engine.invokeFunction("fun")
}
below is the Demo.sc
def fun: String = {
"Rerutn from Fun"
}
Below is the exception at runtime
Exception in thread "main" java.lang.ClassCastException: scala.tools.nsc.interpreter.Scripted cannot be cast to javax.script.Invocable
at DemoApp$.delayedEndpoint$DemoApp$1(DemoApp.scala:13)
at DemoApp$delayedInit$body.apply(DemoApp.scala:5)
at scala.Function0.apply$mcV$sp(Function0.scala:34)
at scala.Function0.apply$mcV$sp$(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:389)
at scala.App.main(App.scala:76)
at scala.App.main$(App.scala:74)
at DemoApp$.main(DemoApp.scala:5)
at DemoApp.main(DemoApp.scala)