1

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?

Community
  • 1
  • 1
Jade Somath
  • 173
  • 4
  • Also possible dup of [How could I call Java code dynamically ?](http://stackoverflow.com/q/588242/139010) – Matt Ball Dec 10 '10 at 04:17

1 Answers1

0

I have never done this myself, but you can write your own ClassLoader and load the bytecodes into the JVM. There is a tutorial on this.

AlanObject
  • 9,613
  • 19
  • 86
  • 142