scala.reflect.internal.FatalError:包scala没有成员Int
我目前正在使用
Scala和Play Framework 2开发一个项目.我想在运行时编译一些Scala代码并从解释器中获取结果.我在互联网上找到了一些例子,最后提出了以下代码:
package controllers import play.api.mvc.{Action,Controller} import javax.script.ScriptEngineManager class Interpreter extends Controller { val interpreter = new ScriptEngineManager().getEngineByName("scala") val settings = interpreter.asInstanceOf[scala.tools.nsc.interpreter.IMain].settings settings.embeddedDefaults[Interpreter] settings.usejavacp.value = true def index = Action { Ok(views.html.interpreter()) } def interpret(input: String) = Action { implicit request => interpreter.eval("1 to 10 foreach println") Ok("Got: " + input) } } object Interpreter 我的问题是我总是从scala.reflect.internal.FatalError得到一个错误:“包scala没有成员Int”,在尝试运行此代码时.经过一些研究,我发现这篇文章中描述了类似的问题: Scala and Play 2.0 Plugins Update 0.38.437 is Out Scala compiler error: package api does not have a member materializeWeakTypeTag 我当前的Scala版本是2.11.4,所以我尝试在我的“build.sbt”文件中切换到不同的“scala-compiler”和“scala-library”版本,但没有成功.就像上面的帖子中提到的那样,它可能是Scala中的一个错误.我想知道是否有人为所述问题找到解决方案或任何解决方法. 在此先感谢您的任何帮助或建议. 解决方法
问题出在使用Scala和Play Framework 2的项目的classpath中.
可以通过从SBT填充类路径到应用程序来解决它: 添加到build.sbt文件: val sbtcp = taskKey[Unit]("sbt-classpath") sbtcp := { val files: Seq[File] = (fullClasspath in Compile).value.files val sbtClasspath : String = files.map(x => x.getAbsolutePath).mkString(":") println("Set SBT classpath to 'sbt-classpath' environment variable") System.setProperty("sbt-classpath",sbtClasspath) } compile <<= (compile in Compile).dependsOn(sbtcp) 码: package controllers import play.api.mvc.{Action,Controller} import javax.script.ScriptEngineManager class Interpreter extends Controller { val interpreter = new ScriptEngineManager().getEngineByName("scala") val settings = interpreter.asInstanceOf[scala.tools.nsc.interpreter.IMain].settings //settings.embeddedDefaults[Interpreter] // not need //settings.usejavacp.value = true // not need val sbtClasspath = System.getProperty("sbt-classpath") settings.classpath.value = s".:${sbtClasspath}" // <-- this line fix the problem def index = Action { Ok(views.html.interpreter()) } def interpret(input: String) = Action { implicit request => interpreter.eval("1 to 10 foreach println") Ok("Got: " + input) } } object Interpreter 在生产中应该使用另一种方案来获得正确的类路径. 可能在命令行中使用’-cp’或’-classpath’键. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |