How to invoke the Scala compiler programmatically?
Asked Answered
J

2

14

I want my Scala code to take a Scala class as input, compile and execute that class. How can I programmatically invoke a Scala compiler? I will be using the latest Scala version, i.e. 2.10.

Juice answered 2/12, 2013 at 7:1 Comment(1)
G
13

ToolBox

I think the proper way of invoking the Scala compiler is doing it via Reflection API documented in Overview. Specifically, Tree Creation via parse on ToolBoxes section in 'Symbols, Trees, and Types' talks about parsing String into Tree using ToolBox. You can then invoke eval() etc.

scala.tools.nsc.Global

But as Shyamendra Solanki wrote, in reality you can drive scalac's Global to get more done. I've written CompilerMatcher so I can compile generated code with sample code to do integration tests for example.

scala.tools.ncs.IMain

You can invoke the REPL IMain to evaluate the code (this is also available in the above CompilerMatcher if you want something that works with Scala 2.10):

  val main = new IMain(s) {
    def lastReq = prevRequestList.last
  }
  main.compileSources(files.map(toSourceFile(_)): _*)
  code map { c => main.interpret(c) match {
    case IR.Error => sys.error("Error interpreting %s" format (c))
    case _ => 
  }}
  val holder = allCatch opt {
    main.lastReq.lineRep.call("$result")
  }

This was demonstrated in Embedding the Scala Interpreter post by Josh Suereth back in 2009.

Grevera answered 2/12, 2013 at 7:59 Comment(0)
N
4

The class to be compiled and run (in file test.scala)

class Test {

   println ("Hello World!")

}

// compileAndRun.scala (in same directory)

import scala.tools.nsc._
import java.io._

val g = new Global(new Settings()) 

val run = new g.Run  

run.compile(List("test.scala"))  // invoke compiler. it creates Test.class.

val classLoader = new java.net.URLClassLoader(
    Array(new File(".").toURI.toURL),  // Using current directory.
    this.getClass.getClassLoader)

val clazz = classLoader.loadClass("Test") // load class 

clazz.newInstance  // create an instance, which will print Hello World.
Numeral answered 2/12, 2013 at 7:52 Comment(1)
when I try to run this I get the following error: error: error while loading Object, Missing dependency 'object scala in compiler mirror', required by /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/rt.jar(java/lang/Object.class) running Scala 2.13.1 and Java 1.8.0_231 do you know how to resolve this?Euchromatin

© 2022 - 2024 — McMap. All rights reserved.