scala.reflect.internal.FatalError: package scala does not have a member Int
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:
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)
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] // 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.
Probably using a '-cp' or '-classpath' key from the command line.