object scala in compiler mirror not found - running Scala compiler programmatically
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.
I hit the same problem.
settings.usejavacp.value = true
solved the problem for me!
@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"