scala.reflect.internal.FatalError: package scala does not have a member Int

送分小仙女□ 提交于 2019-11-30 13:27:03

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!