Compiling your abstract syntax tree transformation with Netbeans IDE

This article explains how to configure the Netbeans IDE to recognize java code the uses compiler internal classes. These classes are not part of the standard JDK and are not recognized by IDEs such as Netbeans, which will refuse to compile the correct code.

The compiler internal classes are used mainly as a hack to define annotations that automatically generate code. The most significant example is the Project Lombok, which transparently generates getters, setters and other code bload. Under the hood, the “Pluggable Annotation Processing API”  (JSR 269) provides a mechanism that exposes internal classes from the java compiler. These classes provide methods that allow us change the abstract syntax tree.

In order to configure Netbeans IDE to recognize the java compiler internal classes, one needs to add the them to the compiler class path. These classes are found within the lib/tools.jar library within the JDK 6 or 7. In order to add this library to the class path, the following procedure may work:

  1. Open the Netbeans IDE, open the Java Platform Manager (menu Tools->Java Platforms). The dialog will list, one the left side, one or more JDKs, one of them marked as default. On the right side, all jar from the compiler bootstrap classpath are listed. We need to add the lib/tools.jar into the list, which is not possible from the dialog.
  2. Click on “Add Platform” and follow the wizard to add the same default platform again, but using another name, for exemple, “JDK 1.7 – javac”.
  3. Close the dialog and the Netbeans IDE.
  4. Open the xml file: C:\Users\name\AppData\Roaming\NetBeans\7.4\config\Services\Platforms\org-netbeans-api-java-Platform\JDK_1.7_-_javac.xml. The path may vary according to your operating system, user name and Netbeans IDE version.
  5. Scan through the xml file for the “sun.boot.class.path” property.
  6. Add the full absolute path of the JDK lib/tools.jar file, don’t forget the “:” separator.
  7. Open the Netbeans IDE again, open the Java Platform Manager.
  8. Make sure that the right side lists the lib/tools.jar file.
  9. Close the dialog.
  10. Open your project properties, go to the libraries tab.
  11. Change the Java Platform field to “JDK 1.7 – javac”.

Some bibliography: