scala - Eclipse 中的测试有效,但 sbt 抛出 MissingRequirementError : object scala. 编译器镜像中未找到运行时

标签 scala sbt

我正在乱搞解析和 scala.tools.nsc.interactive.Global在 Scala 中,我在 sbt 下执行测试时遇到了问题。测试在 Eclipse 中运行良好,均为 JUnitRunnerScalaTest插入。在 Google 上花了很长时间后,我不知道如何解决这个问题。

当我执行sbt test时抛出以下错误:

Exception encountered when attempting to run a suite with class name: compileutils.CompileTest *** ABORTED ***
[info]   java.lang.ExceptionInInitializerError:
[info]   at compileutils.CompileTest$$anonfun$3.apply$mcV$sp(CompileTest.scala:18)
[info]   at compileutils.CompileTest$$anonfun$3.apply(CompileTest.scala:16)
[info]   at compileutils.CompileTest$$anonfun$3.apply(CompileTest.scala:16)
[info]   at org.scalatest.Transformer$$anonfun$apply$1.apply(Transformer.scala:22)
[info]   at org.scalatest.Transformer$$anonfun$apply$1.apply(Transformer.scala:22)
[info]   at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
[info]   at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
[info]   at org.scalatest.Transformer.apply(Transformer.scala:22)
[info]   at org.scalatest.Transformer.apply(Transformer.scala:20)
[info]   at org.scalatest.FunSuiteLike$$anon$1.apply(FunSuiteLike.scala:158)
[info]   ...
[info]   Cause: scala.reflect.internal.MissingRequirementError: object scala.runtime in compiler mirror not found.
[info]   at scala.reflect.internal.MissingRequirementError$.signal(MissingRequirementError.scala:16)
[info]   at scala.reflect.internal.MissingRequirementError$.notFound(MissingRequirementError.scala:17)
[info]   at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:48)
[info]   at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:40)
[info]   at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:61)
[info]   at scala.reflect.internal.Mirrors$RootsBase.getPackage(Mirrors.scala:172)
[info]   at scala.reflect.internal.Mirrors$RootsBase.getRequiredPackage(Mirrors.scala:175)
[info]   at scala.reflect.internal.Definitions$DefinitionsClass.RuntimePackage$lzycompute(Definitions.scala:183)
[info]   at scala.reflect.internal.Definitions$DefinitionsClass.RuntimePackage(Definitions.scala:183)
[info]   at scala.reflect.internal.Definitions$DefinitionsClass.RuntimePackageClass$lzycompute(Definitions.scala:184)
[info]   ...

被测类

package compileutils

import scala.tools.nsc.Settings
import scala.tools.nsc.interactive.Global
import scala.tools.nsc.reporters.ConsoleReporter
import scala.tools.nsc.interactive.Response
import scala.io.Source
import scala.reflect.internal.util.SourceFile
import scala.reflect.internal.util.BatchSourceFile
import scala.reflect.io.AbstractFile
import java.io.File

object Compile {
  val settings = new Settings
  val reporter = new ConsoleReporter(settings)
  val global = new Global(settings, reporter, "Study compile")

  def parse(source: String): Compile.this.global.Tree = {
    val sourceFile = new BatchSourceFile(".", source)
    global.askReload(List(sourceFile), new Response[Unit])

    global.parseTree(sourceFile)
  }

  def loadTypes(source: String): Either[Compile.this.global.Tree, Throwable] = {
    val sourceFile = new BatchSourceFile(".", source)
    val tResponse = new Response[global.Tree]
    global.askReload(List(sourceFile), new Response[Unit])
    global.askLoadedTyped(sourceFile, tResponse)

    tResponse.get
  }

}

测试

package compileutils

import org.scalatest.BeforeAndAfter
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FunSuite
import org.scalatest.Matchers._

@RunWith(classOf[JUnitRunner])
class CompileTest extends FunSuite with BeforeAndAfter {
  val testSource = "class FromString {val s = \"dsasdsad \"}"

  before {}
  after {}

  test("parse") {
    //when
    val tree = Compile.parse(testSource)

    //then
    tree should not be null
  }

  test("typer") {
    //when
    val typ = Compile.loadTypes(testSource)

    //then
    typ should be('left)
  }
}

build.sbt

name := "Compiler study"

version := "0.1"

val scalaBuildVersion = "2.10.3"

scalaVersion := scalaBuildVersion

libraryDependencies += "org.scala-lang" % "scala-compiler" % scalaBuildVersion

libraryDependencies += "org.scala-lang" % "scala-library" % scalaBuildVersion

libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaBuildVersion

libraryDependencies += "org.scalatest" %% "scalatest" % "2.1.0" % "test"

libraryDependencies += "junit" % "junit" % "4.11" % "test"

环境:

sbt launcher version 0.13.0
Scala compiler version 2.10.3 -- Copyright 2002-2013, LAMP/EPFL
javac 1.6.0_45
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=13.10
DISTRIB_CODENAME=saucy
DISTRIB_DESCRIPTION="Ubuntu 13.10"

最佳答案

运行 sbt 时,scala jar 似乎不在您的类路径中 - 请确保在运行 sbt 之前将 scala-library.jar 添加到您的类路径中。


根据您的评论之一,您似乎正在 Windows 上运行。如果类路径包含奇怪的字符或空格,或者权限错误(例如,如果 eclipse 在管理员帐户下运行,而 sbt 不是),那么您可能还会遇到运行时 jar 访问错误。


尝试重新排序您的依赖项列表,将 scala-library 放在 scala-compiler 之前。如果这不起作用,请尝试故障排除建议 here .

关于scala - Eclipse 中的测试有效,但 sbt 抛出 MissingRequirementError : object scala. 编译器镜像中未找到运行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22668012/

相关文章:

scala - 检查我的枚举值中是否存在某些字符串,而没有NoSuchElement异常

scala - json4s - 如何使用多个 FieldSerializer

scala - 在 Scala 中比较两个 Byte[] 的相等性(检查二进制图像数据)

list - scala - 案例类集合列表上的自定义(复杂?)不同函数

SBT 已经失去了使用 ctrl 字符格式化屏幕的能力。为什么?

scala - IntelliJ 中的 SBT 项目和 Scala 模块有什么区别?

java - 使用 git 子模块管理 sbt 依赖

scala - 如何缓存 sbt TaskKey 的结果?

scala - 如何通过新的 cooursier 集成禁用 SBT 中的校验和检查

scala - !不是 String 的成员