Play 2.1中的scala.tools.nsc.IMain [英] scala.tools.nsc.IMain within Play 2.1

查看:119
本文介绍了Play 2.1中的scala.tools.nsc.IMain的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google上搜索了很多,现在完全陷入困境.我知道,还有类似的问题,但请仔细阅读.我已经尝试了所有建议的解决方案,但都没有成功.

I googled a lot and am totally stuck now. I know, that there are similar questions but please read to the end. I have tried all proposed solutions and none did work.

我正在尝试在Play 2.1项目(使用Scala 2.10.0)中使用scala.tools.nsc中的IMain类.

I am trying to use the IMain class from scala.tools.nsc within a Play 2.1 project (Using Scala 2.10.0).

控制器代码

这是代码,我尝试在Websocket中使用IMain.这仅用于测试.

This is the code, where I try to use the IMain in a Websocket. This is only for testing.

object Scala extends Controller {
  def session = WebSocket.using[String] { request =>
    val interpreter = new IMain() 
    val (out,channel) = Concurrent.broadcast[String]
    val in = Iteratee.foreach[String]{ code =>
      interpreter.interpret(code) match {
        case Results.Error =>      channel.push("error")
        case Results.Incomplete => channel.push("incomplete")
        case Results.Success =>    channel.push("success")
      }      
    } 
    (in,out)
  }
}

通过Websocket发送某些消息后,游戏会记录以下错误:

As soon as something gets sent over the Websocket the following error gets logged by play:

Failed to initialize compiler: object scala.runtime in compiler mirror not found.
** Note that as of 2.8 scala does not assume use of the java classpath.
** For the old behavior pass -usejavacp to scala, or if using a Settings
** object programatically, settings.usejavacp.value = true.

Build.scala

object ApplicationBuild extends Build {
  val appName         = "escalator"
  val appVersion      = "1.0-SNAPSHOT"

  val appDependencies = Seq(
    "org.scala-lang" % "scala-compiler" % "2.10.0"
  )

  val main = play.Project(appName, appVersion, appDependencies).settings(    
  )
}

到目前为止我尝试过的事情

所有这些都不起作用:

  • 我已将fork := true包含在Build.scala
  • 一个Settings对象,具有:
    • embeddedDefaults[MyType]
    • usejavacp.value = true
    • I have included fork := true in the Build.scala
    • A Settings object with:
      • embeddedDefaults[MyType]
      • usejavacp.value = true

      我不知道现在该怎么办.

      I dont know what to do now.

      推荐答案

      这里的问题是sbt不会将scala库添加到类路径中. 以下变通办法起作用. 首先在顶层项目目录(应用程序,conf等的父目录)中创建一个文件夹lib,然后在其中复制scala-library.jar

      The problem here is that sbt doesnt add scala-library to the class path. The following workaround works. First create a folder lib in the top project directory(the parent of app,conf etc) and copy there the scala-library.jar

      然后,您可以使用以下代码托管翻译器:

      Then you can use the following code to host an interpreter :

            val settings = new Settings
            settings.bootclasspath.value +=scala.tools.util.PathResolver.Environment.javaBootClassPath + File.pathSeparator + "lib/scala-library.jar"
            val in = new IMain(settings){
                  override protected def parentClassLoader = settings.getClass.getClassLoader()
            }
           val res = in.interpret("val x = 1")
      

      以上内容通过将scala库添加到java类中来创建bootclasspath.播放框架不是问题,它来自sbt.当任何scala项目与sbt一起运行时,也会发生相同的问题.经过一个简单的项目测试.从eclipse运行时,效果很好.

      The above creates the bootclasspath by adding to the java class the scala library. It's not a problem with play framework it comes from the sbt. The same problem occures for any scala project when it runs with sbt. Tested with a simple project. When it runs from eclipse its works fine.

      编辑:链接到展示上述内容的示例项目.`

      这篇关于Play 2.1中的scala.tools.nsc.IMain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆