Scala的ScriptEngine无法第二次评估编译的脚本

时间:2018-11-07 12:57:57

标签: scala scriptengine scala-2.12

我有以下代码:

val engine = new ScriptEngineManager().getEngineByName("scala").asInstanceOf[ScriptEngine with Invocable with Compilable]
  val script = """def incrementN(n: Int)= {
                 |  n+1
                 |}
                 |
                 |incrementN(n.asInstanceOf[Int])"""
  engine.getContext.setAttribute("n", 0,  ScriptContext.ENGINE_SCOPE)
  val compiled = engine.compile(script)
  println(compiled.eval())
  println(compiled.eval())

第一次执行脚本没有问题,但是下一次相同的评估失败,并显示以下信息:

  

线程“ main”中的异常scala.tools.nsc.interpreter.IMain $ ReadEvalPrint $ EvalException:无法加载'$ line6。$ eval':$ line6。$ eval       在scala.tools.nsc。解释器.IMain $ ReadEvalPrint.evalError(IMain.scala:745)       在scala.tools.nsc。解释器.IMain $ ReadEvalPrint.load(IMain.scala:749)       在scala.tools.nsc。解释器.IMain $ ReadEvalPrint.evalClass $ lzycompute(IMain.scala:752)       在scala.tools.nsc。解释器.IMain $ ReadEvalPrint.evalClass(IMain.scala:752)       在scala.tools.nsc。解释器.IMain $ ReadEvalPrint.evalMethod(IMain.scala:795)       在scala.tools.nsc。解释器.IMain $ ReadEvalPrint.call(IMain.scala:730)       在scala.tools.nsc。解释器.IMain $ ReadEvalPrint.callEither(IMain.scala:739)       在scala.tools.nsc。解释器.IMain $ ReadEvalPrint.evalEither(IMain.scala:754)       在scala.tools.nsc。解释器.Scripted $ WrappedRequest。$ anonfun $ evalEither $ 3(Scripted.scala:208)       在scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)       在scala.Console $ .withIn(Console.scala:226)       在scala.tools.nsc。解释器.Scripted $ WrappedRequest。$ anonfun $ evalEither $ 2(Scripted.scala:208)       在scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)       在scala.Console $ .withErr(Console.scala:192)       在scala.tools.nsc。解释器.Scripted $ WrappedRequest。$ anonfun $ evalEither $ 1(Scripted.scala:207)       在scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)       在scala.Console $ .withOut(Console.scala:163)       在scala.tools.nsc.interpreter.Scripted $ WrappedRequest.evalEither(Scripted.scala:206)       在scala.tools.nsc。解释器.Scripted $ WrappedRequest。$ anonfun $ eval $ 1(Scripted.scala:247)       在scala.tools.nsc.interpreter.Scripted.withScriptContext(Scripted.scala:124)       在scala.tools.nsc.interpreter.Scripted $ WrappedRequest.eval(Scripted.scala:225)       在javax.script.CompiledScript.eval(CompiledScript.java:106)

scala版本为2.12.7 Java版本“ 1.8.0_144”

请您帮我弄清楚,这可能是什么问题?

0 个答案:

没有答案