title: Dynamically compiling scala class files at runtime in Scala 2.11
body: <p>I have the following code that works in Scala 2.10 to compile external classes at runtime in Scala</p>
  
  <pre><code>/**
    * Compile scala files and keep them loaded in memory
    * @param classDir Directory storing the generated scala files
    * @throws IOException if there is problem reading the source files
    * @return Classloader that contains the compiled external classes
    */
  @throws[IOException]
  def compileFiles(classDir: String): AbstractFileClassLoader = {
    val files = recursiveListFiles(new File(classDir))
                    .filter(_.getName.endsWith("scala"))
    println("Loaded files: \n" + files.mkString("[", ",\n", "]"))
  
    val settings: GenericRunnerSettings = new GenericRunnerSettings(err =&gt; println("Interpretor error: " + err))
    settings.usejavacp.value = true
    val interpreter: IMain = new IMain(settings)
    files.foreach(f =&gt; {
      interpreter.compileSources(new BatchSourceFile(AbstractFile.getFile(f)))
    })
  
    interpreter.getInterpreterClassLoader()
  }
  </code></pre>
  
  <p>And then elsewhere, I could use the classloader reference to instantiate classes e.g. </p>
  
  <pre><code>val personClass = classLoader.findClass("com.example.dynacsv.PersonData")
  val ctor = personClass.getDeclaredConstructors()(0)
  val instance = ctor.newInstance("Mr", "John", "Doe", 25: java.lang.Integer, 165: java.lang.Integer, 1: java.lang.Integer)
  println("Instantiated class: " + instance.getClass.getCanonicalName)
  println(instance.toString)
  </code></pre>
  
  <p>However the above no longer works as <code>getInterpreterClassLoader</code> method has been removed from <code>scala.tools.nsc.interpreter.IMain</code>. Also, AbstractFileClassLoader has been moved and deprecated. It is no longer allowed to call <code>findClass</code> method in the class loader from an external package.</p>
  
  <p>What is the recommended way to do the above in Scala 2.11? Thanks!</p>