April 10, 2021

How to create basic java plugin using Plugin interface, and run it is using -Xplugin command line argument

We can use Plugin interface to create a plugin that runs during compilation of java files.

You can watch me code this plugin on youtube.


We use com.sun.source.util.Plugin from the jdk. We will have to provide an implementation for init-function of this interface.
And, another thing that is important is to provide implementation for getName-function. The name-string returned by this function is usedĀ 
by the compiler to identify the plugin.

So, starting with code.

I have created a module basic.module inside plugins-source folder.

module basic.module{
    requires jdk.compiler;
    provides com.sun.source.util.Plugin with test.BasicPlugin;

You have to really understand the things that we are doing in the module file.

  1. The name of the module should be the same as the name of the folder.
  2. We need to tell the compiler that we are going to access jdk.compiler, otherwise we will not be able to access Plugin interface, and compilation will fail.
  3. We need to tell the compiler which class implements the Plugin interface, otherwise compiler will not be able to access it and, the plugin will not be called.

So, everything in the module file is very important, you can not skip anything.

Let us implement the Plugin interface, now.

package test;

import com.sun.source.util.Plugin;
import com.sun.source.util.JavacTask;

public class BasicPlugin implements Plugin{
    public String getName() {
        return "basic";

    public void init(final JavacTask javacTask, final String... arguments){
        System.out.println("Basic Plugin Called");

The implementation that you see above is very basic implementation. We are printing a string “Basic Plugin Called”.
This implementation is more that enough to understand the implementation and working of a plugin.

So, in the above code, I have implemented getName-function and init-function. The getName-function provides the name of the plugin. And, init-function provides the functionality of plugin.

The next task is to compile the plugin before use.

So, I create a folder plugins-classes. Inside it, I create another folder with the same name as module basic.module.

Then I compile it with following command .

javac -d plugins-classes/basic.module $(find plugins-source -name "*.java")

-d sets the folder where compiled classes should be saved. The find command finds all the java files inside the plugins-source folder.

Then we create a test java file, just to use this plugin. So, that we can run our plugin.

public class Test{}

Now, it’s time to run this plugin.

javac -Xplugin:basic --processor-module-path plugins-classes/basic.module Test.java

Xplugin:basic , tells the compiler that the name of plugin is basic.

processor-module-path, points the compiler to the plugin directory

you can browse my code onĀ github, for better understanding of folder structure, for better reading exeprience.