Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile and execute Scala code at runtime

Tags:

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?

like image 420
Peter Avatar asked Sep 19 '11 21:09

Peter


People also ask

How do I compile and run a Scala program?

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.

Which command is used to compile and run Scala program?

To compile the code, type "scalac hello_world. scala" and press enter.

Does Scala use compiler or interpreter?

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.


1 Answers

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.

like image 165
Eugene Burmako Avatar answered Sep 21 '22 06:09

Eugene Burmako