使用Global.Run编译Scala代码时出现MissingRequirementError
我正在尝试使用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说:
所以我的问题是如何通过使用Run类以编程方式编译文件,或者我在这里做错了什么? 我试图找出是否可以更改设置以使其正常工作.实际上我需要路径中Scala文件中的类列表,不一定是完全可运行的输出.因此,如果符号仍未解析(如果我可以运行编译器阶段的子集),那就没问题了. 我也是在Writing Scala Compiler Plugins,但如果我可以通过实现编译器运行对象来运行它,我更喜欢这个解决方案.我也偶然发现了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 Filesscalalib").listFiles.foreach(f => { settings.classpath.append(f.getAbsolutePath) settings.toolcp.append(f.getAbsolutePath) }) 解决方法
我通过使用bootclasspath而不是toolcp运行它(感谢pedrofurla的提示):
val settings = new Settings new File("C:Program Filesscalalib").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) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |