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?
Delegate a Scala project build to sbtIn the sbt projects section, select a project for which you want to configure build actions. In the sbt shell section, select the builds option. Click OK to save the changes. Now when you build your project ( Ctrl+F9 ), the build is run in the sbt shell.
To compile the code, type "scalac hello_world. scala" and press enter.
Scala has both a compiler and an interpreter which can execute Scala code. The Scala compiler compiles your Scala code into Java Byte Code which can then be executed by the scala command. The scala command is similar to the java command, in that it executes your compiled Scala code.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With