scalatwitter-util

How do I execute the code and see the results using twitter util-eval


I am trying to compile and execute a "hello world" program using twitter util-eval library. But not able to get results printed.

My intention is not just to compile and execute hello world example, large scala files with dependencies should compile and run.

import com.twitter.util.Eval
object ScalaCompiler3 {
     def main(args: Array[String]): Unit = {
         val eval = new Eval()
         val fileContents = "class Test {\n\n println (\"Hello World!\")\n\n}"
         val compile = eval.apply[Unit](fileContents)
         println(compile)
     }
}

How do I execute the code and see the results? am not able to get proper API documentation for util-eval. Using util-eval_2.11-6.43.0.jar taken from maven


Solution

  • As erip mentioned in the comments, the source string you're compiling makes a class, Test, but since you never instantiate the class it never runs the code within.

    If you replace:

    val fileContents = "class Test {\n\n println (\"Hello World!\")\n\n}"
    

    with:

    val fileContents = "println(\"Hello World!\")\n\n}"
    

    That will execute the code you're looking to execute. Best of luck with your project!