Possible Duplicates:
using eval in Java
Loading external source code and using them internally (by re-compiling or something)
I want to simulate an 'eval' in Java such that the function takes in a String (some source code), compiles it, runs it, and spits back the return value.
I know I can compile it using the JavaCompiler API, but I am unsure the best way to actual run the code like this. Runtime.exec feels too heavy for this task... I don't want to spawn another java process each time I run this 'eval' function just to evaluate '30+20' or something. This would be slow and resource intensive.
Finally, this must be able to run ANY java code, so relying on a scripting language evaluator inside Java won't do (ex. if the input is more complex Java than just '30+20')
Any alternatives?