[编辑:添加评论中要求的精度]
我正在尝试以编程方式运行编译器并得到“缺少依赖项'编译器镜像中的对象 scala'”错误。我在编译器镜像中找不到这个帖子对象 scala - 以编程方式运行 Scala 编译器(但它只解释了如果我们从 sbt 而不是从外部启动程序时如何解决问题)。
所以我在这里复制/粘贴示例:
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)
}
以及从 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)
}
我正在尝试通过将来自 sbt 的 fullClassPath 值作为类路径传递来直接从终端启动这两个版本:
java -cp /home/lorilan/path-to-my-project/target/scala-2.11/classes:/home/lorilan/.ivy2/cache/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/lorilan/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.11.6.jar p.Playground
java -version 在哪里给出
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)
每次我收到以下错误:
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)
所以我的问题是双重的:
有人可以向我解释上述帖子中显示的“魔术”是如何工作的吗?(我没有得到 embeddedDefaults 方法的注释)
有人可以给出一个同样神奇的技巧来从终端运行操场吗?