Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Options for dynamic compilation in Java 5

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.

like image 982
Ryan Emerle Avatar asked Feb 19 '09 17:02

Ryan Emerle


People also ask

What is dynamic compilation in Java?

Dynamic compilation is a process used by some programming language implementations to gain performance during program execution. Although the technique originated in Smalltalk, the best-known language that uses this technique is Java.

What are the commands used for compilation and execution of Java programs?

Type 'javac MyFirstJavaProgram. java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set). Now, type ' java MyFirstJavaProgram ' to run your program.

How many compilers are there in Java?

Java compilers include the Java Programming Language Compiler (javac), the GNU Compiler for Java (GCJ), the Eclipse Compiler for Java (ECJ) and Jikes. Programmers typically write language statements in a given programming language one line at a time using a code editor or an integrated development environment (IDE).

Which of the following tool is used to compile Java code?

The javac tool reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files. It can also process annotations in Java source files and classes.


1 Answers

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.

like image 137
MBCook Avatar answered Sep 28 '22 01:09

MBCook