On-The-Fly, In-Memory Java Code Compilation for Java 5 and Java 6

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

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.

java in-memory on-the-fly class compilation (and loading)

I ended up using Bean Shell. It's not perfect, but it solved 99% of the issue.

Compiling java Code Available In A String within another java code

You want to have a look at javax.tools.JavaCompiler and related classes.
The documentation contains examples of how to use them.

Note that the java compiler will only work if you have a JDK installed. A JRE is not enough.

Include Javac in Java applet

Java compiler is always with you. You should use java.lang.Compiler class. But I believe you will have issues with SecurityManager.

If I were you I'd prefer to compile code on server side. It means that I'd develop servlet that is able to get the java source and compile it. The resulted java class should be available over HTTP under path that is configured to be accessible by applet, so your applet will be able to run this class after compilation.

Compile code fully in memory with javax.tools.JavaCompiler

I've run the above code in Mac OS Java 7. None of them works. So i wrote one
https://github.com/trung/InMemoryJavaCompiler

StringBuilder source = new StringBuilder()
.append("package org.mdkt;\n")
.append("public class HelloClass {\n")
.append(" public String hello() { return \"hello\"; }")
.append("}");

Class<?> helloClass = InMemoryJavaCompiler.compile("org.mdkt.HelloClass", source.toString());


Related Topics



Leave a reply



Submit