Options for dynamic compilation in Java 5
Asked Answered
D

3

7

Are there any options other than Janino for on-the-fly compiliation and execution of Java code in v5? I know v6 has the Compiler API, but I need to work with the v5 VM.

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

Daisey answered 19/2, 2009 at 17:5 Comment(2)
Is there a reason you aren't interested in Janino? Are you just looking for the other options or is there a specific problem it's causing?Homburg
Janino is great, but I'm looking for more advanced language features that are not supported by Janino. Actually Janino is what we're currently using.Daisey
H
6

What you want is something like Janino. We've used it for years. You give it (near standard) code and it gives you the classes so you can use them. It actually has quite a few different modes and supports the 1.5 syntactic sugar and auto-boxing and such.

If you call javac, not only will you have to be ready for anything it does, you'll then have to handle putting the class in the right place or making an additional classloader.

Janino is very easy. It should be exactly what you are looking for.

Homburg answered 19/2, 2009 at 18:51 Comment(2)
+1 just because you were so passionate about the solution that you missed the first sentence.Daisey
Unfortunately janino have bad generic support. It can read generics but can not write geneircs.Vaudeville
I
5

Invoking javac programatically:

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);
Isomorph answered 19/2, 2009 at 17:49 Comment(2)
This requires writing out to a file. Unfortunately the variety of deployment environments makes this prohibitive. +1 For the good lead, though.Daisey
What would you need to have the end result as?Isomorph
F
3

All app servers do this for JSP for ever, so obviously it is possible. Checkout tomcat source code maybe?

Frankish answered 19/2, 2009 at 17:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.