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)](https://stackoverflow.com/questions/688098/compile-to-java-bytecode-without-using-java) I have become aware of the following possible solutions:

* [ASM](http://asm.objectweb.org/)
* [BCEL](http://jakarta.apache.org/bcel/)
* [Trove](http://teatrove.sourceforge.net/trove.html)

I would love a comparison of these as well as other suggestions for solving the Java compile at runtime phase.
['https://Stackoverflow.com/questions/1064259', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/101715/']
Save yourself the hassle and use [BeanShell](http://www.beanshell.org/) as alluded to here [Executing java code given in a text file](https://stackoverflow.com/questions/1057796/executing-java-code-given-in-a-text-file).

> 
> **What is BeanShell?**
> 
> 
> BeanShell is a small, free, embeddable
>  Java source interpreter with object
>  scripting language features, written
>  in Java. BeanShell dynamically
>  executes standard Java syntax and
>  extends it with common scripting
>  conveniences such as loose types,
>  commands, and method closures like
>  those in Perl and JavaScript.
> 
> 
> You can use BeanShell interactively
>  for Java experimentation and debugging
>  as well as to extend your applications
>  in new ways. Scripting Java lends
>  itself to a wide variety of
>  applications including rapid
>  prototyping, user scripting extension,
>  rules engines, configuration, testing,
>  dynamic deployment, embedded systems,
>  and even Java education.
> 
> 
> BeanShell is small and embeddable, so
>  you can call BeanShell from your Java
>  applications to execute Java code
>  dynamically at run-time or to provide
>  extensibility in your applications.
>  Alternatively, you can use standalone
>  BeanShell scripts to manipulate Java
>  applications; working with Java
>  objects and APIs dynamically. Since
>  BeanShell is written in Java and runs
>  in the same VM as your application,
>  you can freely pass references to
>  "live" objects into scripts and return
>  them as results.
> 
> 
> In short, BeanShell is dynamically
>  interpreted Java, plus a scripting
>  language and flexible environment all
>  rolled into one clean package.
> 
> 
>
you can fork a process like this

```
Process p = Runtime.getRuntime().exec("java -classpath "..." SomeClassContainingMain ...other arguments);       

        //you need to consume the outputs of the command if output/error is large otherwise the process is going to hang if output/error buffer is full. and create a seperate thead for it (not created here).
        log.debug("PROCESS outputstream : " + p.getInputStream() );
        log.debug("PROCESS errorstream : " + p.getErrorStream());           
    p.waitFor(); // Wait till the process is finished

```

and can compile and run it.