# Compiling Queries to Java code

Saxon-SA (from release 8.9) provides the capability to compile a query to Java source code. The Java source code can then be compiled to byte code in a class file, which can be executed in the same way as any other Java program.

This is achieved by adding a code-generation phase to the existing query analysis phase. The query analyzer generates an optimized expression tree in memory. In interpreted execution, the run time system evaluates this expression tree directly. With compiled execution, the expression tree is compiled to Java code. The Java code makes calls on the Saxon run-time to perform operations such as navigating the source tree and serializing the output.

The following sections discuss Java code generation in more detail.

- Limitations

- Java Code Generation from the Command Line

- Running Compiled Code from the Command Line

- Running a Compiled Query as a Servlet

- Calling Compiled Queries from an Application

Next