11

Running w/ a simple SBT project w/ Java 7 (details below) and invoking sbt run at the command line (no IntelliJ or anything)

source

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: 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)
Cœur
  • 37,241
  • 25
  • 195
  • 267
adelbertc
  • 7,270
  • 11
  • 47
  • 70

4 Answers4

10

This is the one where you have to say:

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)
}

That took me a couple of minutes. That method name, "embeddedDefaults", is as cryptic as any to come out of sbt.

The comment on MutableSettings (which suggests a side effect):

  /** 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.*/

The indentation is as in the source code.

som-snytt
  • 39,429
  • 2
  • 47
  • 129
4

I hit the same problem.

settings.usejavacp.value = true

solved the problem for me!

Thamme Gowda
  • 11,249
  • 5
  • 50
  • 57
  • 2
    I had to upvote this. Not only is it the simplest option listed, it worked for me while the top voted answer did not. – Mark Lewis Jun 19 '17 at 02:18
3

@som-snytt solution worked for me on a clean sbt project. It didn't work on an akka-http project. this is the manual solution I've found (hardcoded path. One should adjust it to his env or put it in conf file)

It is just telling the compiler where to find scala libs for compilation

    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"
ozma
  • 1,633
  • 1
  • 20
  • 28
  • that seems suspicious to me, but I'll take a look . – som-snytt Oct 31 '16 at 20:20
  • Actually this only worked for me. settings.embeddedDefaults didn't help. Also I just read MANIFEST Class-Path in my case and used it like `private val classPath = getManifestAttr("Class-Path","").replace(' ',':') private val settings = new Settings settings.usejavacp.value = true settings.classpath.append(classPath) settings.bootclasspath.append(classPath) ` – abdolence Nov 12 '17 at 21:37
0

I resole it, beacuse the maven dependence error:

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

I remove this dependency, and then successful!!

Paul Chu
  • 1,249
  • 3
  • 19
  • 27
geektcp
  • 1
  • 2