【问题标题】:MissingRequirementError when Compiling Scala code with Global.Run使用 Global.Run 编译 Scala 代码时出现 MissingRequirementError
【发布时间】:2012-12-03 10:55:05
【问题描述】:

我正在尝试使用 Global.Run 的实例以编程方式编译 Scala 文件:

val settings = new Settings                 
val reporter = new ConsoleReporter(settings)
val compiler = new Global(settings, reporter)
val run = new compiler.Run // MissingRequirementError
run compile List(path)

不幸的是,我收到了MissingRequirementError 说:

在编译器镜像中找不到对象 scala.runtime

所以我的问题是如何使用Run 类以编程方式编译文件,或者我在这里做错了什么?

我试图弄清楚是否可以更改settings 以使其正常工作。实际上,我需要path 处的 Scala 文件中的类列表,不一定是完全可运行的输出。因此,如果符号仍未解析(如果我可以运行编译器阶段的一个子集),那会很好。

我也在Writing Scala Compiler Plugins,但如果我可以通过实例化 Compiler Run 对象来运行它,我更喜欢这个解决方案。我还偶然发现了Is the Scala compiler reentrant?(类似的代码,不同的问题),这让我觉得它可能会按照我的想法工作。

编辑 1:将 Scala JAR 添加到 toolcp(只是带有绝对路径的示例代码!)

根据评论,我将 scalac.bat 的类路径填充脚本改编为我的 Scala 代码:

// scalac.bat
// if "%_TOOL_CLASSPATH%"=="" (
//   for %%f in ("!_SCALA_HOME!\lib\*") do call :add_cpath "%%f"
//   for /d %%f in ("!_SCALA_HOME!\lib\*") do call :add_cpath "%%f"
// )
new File("C:\\Program Files\\scala\\lib").listFiles.foreach(f => {
  settings.classpath.append(f.getAbsolutePath)
  settings.toolcp.append(f.getAbsolutePath)
})

【问题讨论】:

  • 已将所有 jars 添加到您的类路径中?检查 scalac 并查看它如何设置要编译的类路径。
  • @pedrofurla 我已将其附加到classpath,但尚未附加到toolcp。我添加了一些代码,但没有帮助(同样的错误)。编辑了问题。感谢阅读和评论!

标签: scala scala-2.10 scala-compiler


【解决方案1】:

我通过使用 bootclasspath 而不是 toolcp 来运行它(感谢 pedrofurla 的提示):

val settings = new Settings
new File("C:\\Program Files\\scala\\lib").listFiles.foreach(f => {
  settings.classpath.append(f.getAbsolutePath)
  settings.bootclasspath.append(f.getAbsolutePath)
})

private val reporter = new ConsoleReporter(settings)
private val compiler = new Global(settings, reporter)

val run = new compiler.Run
run compile List(path)

编译器现在尝试编译文件。然而,这似乎并不是scalac.bat 所做的。它以-cp 开头,这是普通的类路径,而bootclasspath 在控制台上以-bootclasspath 传递,如StandardScalaSettings trait 所示:

val bootclasspath = PathSetting ("-bootclasspath", "Override location of bootstrap class files.", Defaults.scalaBootClassPath)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多