Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compile and deploy a java class at runtime?

I am in the process of writing a rule engine that performs simple assignments as determined by conditional constructs. It is a prerequisite of the project that the rules be in XML format. I have modeled my XML schema to resemble simple code blocks. I wish to parse the XML and to then transform it into Java code. I then wish to compile (and run) this code at runtime. Doing so would mean that my rule engine no longer acts as an interpreter but executes native Java Byte Code.

I have figured out the parsing stage and more or less the Java code generation phase. I would now like to figure out the last phase - the compile at runtime phase.

Following this thread: Compile to java bytecode (without using Java) I have become aware of the following possible solutions:

  • ASM
  • BCEL
  • Trove

I would love a comparison of these as well as other suggestions for solving the Java compile at runtime phase.

like image 746
Yaneeve Avatar asked Jun 30 '09 15:06

Yaneeve


People also ask

How do you compile and run a Java class?

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.

Can we compile Java program with JRE?

No you can't develop java programs only with JRE. You will need JDK for compiling your programs. JRE provides only runtime environment,but JDK is something you will need to compile your code to make them executable by your JRE .

How is compiling and running of Java program done?

In Java, programs are not compiled into executable files; they are compiled into bytecode (as discussed earlier), which the JVM (Java Virtual Machine) then executes at runtime. Java source code is compiled into bytecode when we use the javac compiler. The bytecode gets saved on the disk with the file extension .


1 Answers

You could transform it into Clojure code, and the Clojure compiler will turn it into bytecode for you.

like image 94
kwatford Avatar answered Sep 18 '22 18:09

kwatford