Compile and execute Scala code at runtime

馋奶兔 提交于 2019-12-20 12:07:37

问题


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?


回答1:


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.




回答2:


I recommend you twitter-util's Eval



来源:https://stackoverflow.com/questions/7477589/compile-and-execute-scala-code-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!