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?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.