scala.reflect.internal.FatalError: 包 scala 没有成员 Int [英] scala.reflect.internal.FatalError: package scala does not have a member Int
问题描述
我目前正在开发一个使用 Scala 和 Play Framework 2 的项目.我想在运行时编译一些 Scala 代码并从解释器中获取结果.我在网上找了一些例子,最后得出了如下代码:
I am currently working on a project using Scala and Play Framework 2. I want to compile some Scala code during runtime and get the result from the interpreter. I found some examples on the internet and finally came up with the following code:
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: "package scala does not have a member Int"
收到错误消息.经过一番研究,我发现了这篇文章中描述的类似问题:
My problem is that I always get an error from scala.reflect.internal.FatalError: "package scala does not have a member Int"
, when trying to run this code. After some research I found similar problems described in this posts:
Scala 和 Play 2.0 插件更新 0.38.437 已发布
Scala 编译器错误:包 api 没有没有成员 materializeWeakTypeTag
我当前的 Scala 版本是 2.11.4,所以我尝试在我的build.sbt"文件中切换到不同的scala-compiler"和scala-library"版本,但是使用没有成功.就像在上面的帖子中提到的那样,它可能是 Scala 中的一个错误.我想知道是否有人对所描述的问题有解决方案或任何解决方法.
My current Scala version is 2.11.4, so I tried to switch to a different "scala-compiler" and "scala-library" versions in my "build.sbt" file, but with no success. Like it was mentioned in the posts above it is probably a bug in Scala. I wondered if somebody has a solution or perhaps any workaround for the described problem.
预先感谢您的任何帮助或建议.
Thanks in advance for any help or advice.
推荐答案
问题出在使用 Scala 和 Play Framework 2 的项目中的类路径中.可以通过将类路径从 SBT 填充到应用程序来解决:
The issue is in classpath in projects using Scala and Play Framework 2. It can be resolved by populating classpath from SBT to the application:
添加到 build.sbt 文件:
Add to build.sbt file:
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
在生产中应该使用另一个场景来获得正确的类路径.
In production should be used another scenario to get a correct classpath.
可能使用命令行中的-cp"或-classpath"键.
Probably using a '-cp' or '-classpath' key from the command line.
这篇关于scala.reflect.internal.FatalError: 包 scala 没有成员 Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!