Dynamic Compilation Options in Java 5

Are there any options other than Janino to compile and execute Java code on the fly? I know that v6 has a compiler API, but I need to work with VM v5.

I essentially need to take a string containing the full Java class, compile it, and load it into memory.

+5
source share
3 answers

What you want is something like Janino . We have used it for many years. You give it (near the standard) code, and it gives you classes so you can use them. In fact, it has quite a few different modes and supports 1.5 syntactic sugar and auto-boxing, etc.

If you call javac, you will not only be ready for anything you need, you will have to process the class in the right place or do an additional class loader.

Yanino is very easy. This should be exactly what you are looking for.

+6
source

Request javac programmatically:

http://www.juixe.com/techknow/index.php/2006/12/12/invoke-javac-at-runtime/

  com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main ();  

    String [] options = new String [] 
    {  
       "-classpath", classpath, "-d", outputDir, filename  
    };

    javac.compile (options);
+5
source

All application servers do this for JSP forever, so obviously this is possible. Perhaps you can check the tomcat source code?

+2
source

All Articles