Java Compiler API


The JAVA Compiler API is an important Application Programming Interface for developers. The primary and all-important task to the Compiler API is to provide users systematic and programmatic access to the JAVA compiler that is responsible for compiling Java source codes. The Compiler API was brought into effect from Java version 1.6. After several efforts, this consolidated a standard API for developers to work with. With the advent of this API, they no longer had to rely on classes from the* to call the Java Compiler.

What is the Java Compiler API and its related structure?

As mentioned before, the Java Compiler API provides users with a tool to invoke the compiler to be able to run JAVA source codes from files or application programs. This API falls under the java.compiler module. In general, this module of JAVA controls functionalities related to the language construct, annotation processing, and the compiler. The module governs the compiler tools that will be called when a certain application code is being processed.

Java Compiler API

If you want to directly invoke the Java Compiler API in your code, you will have to import the package into your project. The classes and interfaces included in the said package will allow you to invoke and run the compiler from your own program. A Service Provider Interface (SPI) is also provided by the package that mainly deals with the diagnostics recommended by the compiler at run time.


When a compiler is invoked through code, it does the work of providing a full diagnostic of the code. It acts as a virtual doctor prescribing errors to the listener if one is provided. In case the listener is not there, the diagnostics will be sent to the default output that is System.err. If a case arises where a particular error reported does not fit the diagnostic structure of a listener, it is reported to the System.err display.

When to use the Java Compiler API

The main function of the Java Compiler API, as we have seen through the course of this article, is to invoke the compiler such that Java source codes can be compiled successfully and developers need not have to resort to undocumented classes to do the same. Therefore, this purpose becomes the primary use of this API. If you are a developer yourself or otherwise and want to run a certain piece of code that requires the compiler being invoked from within your code, you must use the Java Compiler API.


As programs that have been processed and cleared by compilers have the capacity to sustain itself as self-sufficient units, invoking the compiler always helps if you have a relevant situation at hand. Your application code will be aware of the rectifications (if present) that need to be tackled. As the process of using the API just requires a few package imports, the task is not daunting at all.


The only significant disadvantage is that compile times for applications containing huge volumes of code might compromise speed and efficiency. One must be careful when invoking compiler apis as smaller classes that have been executed prior can be exempted to save on compile time.

Sample Code

/*We will be compiling a simple class that will just contain a print statement in it. The way of knowing whether the class has passed is when the compiler will print out the statement mentioned in the test class. */

//Test Class
//The following class will be saved under the name

public class Test {
public static void main (String args []) {
System.out.println(“The Test has been successful”);

//Now we will write the class that will invoke the compiler
//Most of the code below is adhering to the packages and its constructs.

public class Tester{
final DiagnosticCollector <JavaFileObject> diag = new DiagnosticCollector<>();
final StandardJavaFileManager man = compiler.getStandardFileManager( diag, null, null);

final File file = new File(CompilerExample.class.getResource(“”/”). to URI());

final Iterable < extends JavaFileObject > source = manager.getJavaFileObjectsFromFiles(Array.asList(file));
//Finally we will invoke the compiler that will compile the Test class created above
final CompilationTask flag = compiler.getTask(null, man, diag, null, null, sources);



The Test has been successful


  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.

    View all posts


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.