Compile and execute Scala code at runtime

Peter picture Peter · Sep 19, 2011 · Viewed 11.2k times · Source

Is is possible to compile and execute scala code as a string at runtime either in Scala or in Java?

My idea is to build a DSL using Scala then let Java programmers use the DSL inside Java.

I heard that the class scala.tools.nsc.Interpreter can do something like that, but when I imported it inside my scala file, I got "object tools is not a member of package scala."

So could anybody give me a hint?

Answer

Eugene Burmako picture Eugene Burmako · Sep 16, 2012

In 2.10.0 we expose Scala reflection API, which among everything else includes a runtime compilation facility. More details can be found here: Generating a class from string and instantiating it in Scala 2.10.