javacompilationruntimeruntime-compilation

On-the-fly, in-memory java code compilation for Java 5 and Java 6


How can I compile java code from an arbitrary string (in memory) in Java 5 and Java 6, load it and run a specific method on it (predefined)?

Before you flame this, I looked over existing implementations:

I'm sure this has been solved before, but I can't find anything that looks even half-production quality on google (except jci, which, as I've said before, I haven't managed to use).

Edit:

Edit2: For now, I'm content with BeanShell "evaluate". Apparently it does everything I need it to (get a string, evaluate it in the context of the 'current' classpath. It does miss some of Java 5 features, but it can use enums (not define) and compiled 'generic' (erased) classes, so it should be enough for what I want.

I don't want to mark the answer as accepted yet since I do hope for a better solution to come up.

Edit3: Accepted the beanshell suggestion - it really works wonderfully.


Solution

  • If you're not completely tied to compiling, solutions like Beanshell, groovy and the other scripting languages are easily embedded (in-fact, java has built-in support for plugging in a scripting language so your code doesn't even know what language the script is written in)

    Beanshell should run any 100% java code IIRC, and I believe Groovy can run most java code--possibly all.