object scala in compiler mirror not found - running Scala compiler programatically [no sbt - no IDE]

510 views Asked by At

[EDIT : add precisions asked in comments]

I'm trying to run the compiler programatically and get a "Missing dependency 'object scala in compiler mirror'" error. I found this post object scala in compiler mirror not found - running Scala compiler programatically (but it only explains how to solve the problem if we launch the program from sbt not from outside).

So I copy/paste here the example :

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

and the solution that make it work when launched from SBT :

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

I'm trying to launch both version directly from the terminal by passing as classpath the fullClassPath value from sbt:

java -cp /home/lorilan/path-to-my-project/target/scala-2.11/classes:/home/lorilan/.ivy2/c‌​ache/org.scala-lang/scala-compiler/jars/scala-compiler-2.11.6.jar:/home/lorilan/.‌​ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.6.jar:/home/loril‌​an/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.11.6.jar p.Playground 

where java -version gives

java version "1.7.0_79" 
OpenJDK Runtime Environment (IcedTea 2.5.5) (Arch Linux build 7.u79_2.5.5-1-x86_64)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode) 

Each time I get the following error :

error: error while loading Object, Missing dependency 'object scala in compiler mirror', required by /usr/lib/jvm/java-7-openjdk/jre/lib/rt.jar(java/lang/Object.class)
Exception in thread "main" 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:1392)
at scala.tools.nsc.Global$Run.<init>(Global.scala:1216)
at puck.Playground$.main(Playground.scala:15)
at puck.Playground.main(Playground.scala)

So my question is twofold :

  • can someone explain to me how works the "magic trick" showed in the aforementioned post ? (I don't get the commentary of the embeddedDefaults method)

  • can someone give an equally magic trick to run the playground from the terminal ?

0

There are 0 answers