Compiling a Java Class in memory with `lombok` annotations and Java JDK 8

Thanks to Holger, I successfully solved the problem.

The issue was caused by the absence of tools.jar in the class path.
This is due to the fact that Eclipse by default recognises the Java environment as a JRE instead of a JDK.

On top of that, Java JDK may – or may not, depending on which version you have – have the tools.jar file.

If you have Java 7 or 8, you should have such library in $JAVA_HOME/lib/tools.jar.

If you have Java 6, the file is not present but the same functionality is provided by $JAVA_HOME/Classes/classes.jar.

The compiler is a feature added with Java 6, so if you want to use it and you have an older version of Java, you should update your environment first.

Now, there are several ways to include tools.jar (or classes.jar) into your project’s class path; since I use gradle, I decided to introduce it as a dependency, as you can see in the following snippet of code:

dependencies {
    compile files("${System.properties['java.home']}/../lib/tools.jar")
    compile 'org.projectlombok:lombok:1.14.4'
    testCompile 'junit:junit:4.11'
}

Hope this little explanation might help other people facing a similar problem!

Cheers!

Leave a Comment