未找到编译器镜像中的对象scala - 以编程方式运行Scala编译器

时间:2015-01-14 01:06:36

标签: scala scalac scala-compiler

使用简单的SBT项目运行w / Java 7(详情如下)并在命令行调用sbt run(无IntelliJ或任何其他内容)

import scala.tools.nsc.{ Global, Settings }

object Playground extends App {
  val compiler = new Global(new Settings())
  val testFiles = List("Test.scala")
  val runner = new compiler.Run()
  val result = runner.compile(testFiles)
  println(result)
}

错误

error: error while loading Object, Missing dependency 'object scala in compiler mirror', required by /Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home/jre/lib/rt.jar(java/lang/Object.class)
[error] (run-main-0) scala.reflect.internal.MissingRequirementError: object scala in compiler mirror not found.
scala.reflect.internal.MissingRequirementError: object scala in compiler mirror not found.
    at scala.reflect.internal.MissingRequirementError$.signal(MissingRequirementError.scala:17)
    at scala.reflect.internal.MissingRequirementError$.notFound(MissingRequirementError.scala:18)
    at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:53)
    at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:66)
    at scala.reflect.internal.Mirrors$RootsBase.getPackage(Mirrors.scala:173)
    at scala.reflect.internal.Definitions$DefinitionsClass.ScalaPackage$lzycompute(Definitions.scala:161)
    at scala.reflect.internal.Definitions$DefinitionsClass.ScalaPackage(Definitions.scala:161)
    at scala.reflect.internal.Definitions$DefinitionsClass.ScalaPackageClass$lzycompute(Definitions.scala:162)
    at scala.reflect.internal.Definitions$DefinitionsClass.ScalaPackageClass(Definitions.scala:162)
    at scala.reflect.internal.Definitions$DefinitionsClass.init(Definitions.scala:1388)
    at scala.tools.nsc.Global$Run.<init>(Global.scala:1053)
    <etc...>

build.sbt

scalaVersion := "2.11.4"

val scalaV = "2.11.4"

libraryDependencies ++= Seq(
  "org.scala-lang"    %   "scala-compiler"      % scalaV,
  "org.scala-lang"    %   "scala-library"       % scalaV,
  "org.scala-lang"    %   "scala-reflect"       % scalaV
)

的java

$ java -version
java version "1.7.0_60-ea"
Java(TM) SE Runtime Environment (build 1.7.0_60-ea-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)

4 个答案:

答案 0 :(得分:9)

这是你必须说的:

trait Probe

object Playground extends App {
  //val compiler = new Global(new Settings())
  val s = new Settings()
  s.embeddedDefaults[Probe]
  val compiler = new Global(s)
  val testFiles = List("Test.scala")
  val runner = new compiler.Run()
  val result = runner.compile(testFiles)
  println(result)
}

这花了我几分钟。那个方法名称“embeddedDefaults”和sbt一样神秘。

MutableSettings的评论(暗示副作用):

  /** Initializes these settings for embedded use by type `T`.
  * The class loader defining `T` should provide resources `app.class.path`
  * and `boot.class.path`.  These resources should contain the application
  * and boot classpaths in the same form as would be passed on the command line.*/

缩进与源代码一样。

答案 1 :(得分:4)

我遇到了同样的问题。

settings.usejavacp.value = true

为我解决了这个问题!

答案 2 :(得分:3)

@ som-snytt解决方案在一个干净的sbt项目上为我工作。它没有在akka-http项目上工作。这是我发现的手动解决方案(硬编码路径。一个应该调整到他的环境或把它放在conf文件中)

它只是告诉编译器在哪里找到用于编译的scala库

    val settings = new Settings()
    //didn't need this one:// settings.embeddedDefaults[Probe]
    settings.classpath.value = "/home/oz/.ivy2/cache/org.scala-lang/scala-compiler/jars/scala-compiler-2.11.8.jar:/home/oz/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.8.jar:/home/oz/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.11.8.jar:/home/oz/.ivy2/cache/org.scala-lang.modules/scala-xml_2.11/bundles/scala-xml_2.11-1.0.4.jar:/home/oz/.ivy2/cache/org.scala-lang.modules/scala-parser-combinators_2.11/bundles/scala-parser-combinators_2.11-1.0.4.jar"
    settings.bootclasspath append "/home/oz/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.8.jar:/home/oz/.ivy2/cache/org.scala-lang/scala-compiler/jars/scala-compiler-2.11.8.jar:/home/oz/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.11.8.jar:/home/oz/.ivy2/cache/org.scala-lang.modules/scala-xml_2.11/bundles/scala-xml_2.11-1.0.4.jar:/home/oz/.ivy2/cache/org.scala-lang.modules/scala-parser-combinators_2.11/bundles/scala-parser-combinators_2.11-1.0.4.jar:/home/oz/.ivy2/cache/jline/jline/jars/jline-2.12.1.jar"

答案 3 :(得分:0)

我解决了它,因为maven依赖错误:

<dependency>
    <groupId>com.haizhi.spark</groupId>
    <artifactId>spark-assembly</artifactId>
    <version>1.6.1</version>
</dependency>

我删除了这个依赖项,然后成功!!