MissingRequirementError when compiling Scala code with Global.Run

I am trying to compile Scala files programmatically using an instance of Global.Run :

 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) 

Unfortunately, I get a MissingRequirementError message:

scala.runtime object not found in compiler mirror

So my question is : how can I compile a file programmatically using the Run class, or what am I doing wrong here?

I tried to find out if I can change the settings to make it work. Actually, I need a list of classes that are in the Scala file in path , not necessarily fully executable output. Therefore, it would be great if the characters remained unresolved (if I could run a subset of the compiler phases).

I am also on the Writing Scala Compiler Plugins , but if I can run it by running the compiler object, I would prefer this solution. I also stumbled upon Is Scala a compiler of a reentrant? (similar code, another question) that makes me think that this might work the way I think.

Edit 1: Added Scala JARs to toolcp (only sample code with absolute path!)

According to the comment, I adapted scalac.bat script class population to my Scala code:

 // 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) }) 
+4
source share
1 answer

I ran it using bootclasspath instead of toolcp (thanks to pedrofurla hint):

 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) 

The compiler is trying to compile the files now. However, this is not exactly what scalac.bat does. It starts it with -cp , which is the usual class path, while the bootclasspath is passed using -bootclasspath in the console, as seen in StandardScalaSettings trait:

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

All Articles