Skip to content

Instantly share code, notes, and snippets.

@wfaler
Last active August 29, 2015 14:20
Show Gist options
  • Save wfaler/498e61d0f4879b983434 to your computer and use it in GitHub Desktop.
Save wfaler/498e61d0f4879b983434 to your computer and use it in GitHub Desktop.
compile scala dynamically
import scala.tools.nsc._
import java.io._
val scalaLibraryPath = "/Users/wfaler/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.2.jar"
val s = new Settings()
s.bootclasspath.append(scalaLibraryPath)
s.classpath.append(scalaLibraryPath)
val g = new Global(s)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment