Fix Eclipse not finding javax.annotation

I recently started playing with Java again and I had to get up to date with the recent tooling. Gradle was my first target. While playing with it, I got really annoyed at some point: I had a working Gradle build, but Eclipse was telling me that it could not find javax.annotation.CheckForNull and a few other friends. Here is how to fix Eclipse not finding javax.annotation.

In my case, some generated code I was using needed @Nonnull and @CheckForNull. Both of these were introduced in JSR-305 (aka Annotations for Software Defect Detection).

Step 1 – Ensure that you have the library

The easiest implementation I could find was the one shipped with FindBugs (com.google.code.findbugs:jsr305). In order to include this dependency into your project, simply modify your build.gradle file to add the following:

 

In my specific case, I was already using org.anarres.gradle:gradlesablecc-plugin:1.0.4, and this already requires jsr305. Therefore, I did not have to manually add it.

 

Figure 1: Gradle dependencies output

In order to validate your dependency, you can get the dependency tree from Gradle. To do so, get a command prompt open in your project root directory. In there, simply run: .\gradlew.bat dependencies –configuration testCompileClasspath. Since I am using other dependencies, the output from this command in my project is what is shown in figure 1.

 

Step 2 – Refresh your Eclipse project

This step is where I lost quite a bit of time …

If you are using Eclipse Buildship, you can refresh the project by simply right-clicking it, and in the menu hit Gradle->Refresh Gradle Project. This will update your Eclipse project to ensure that all classpath setting and other stuff is valid.

Sources

Tagged , . Bookmark the permalink.

Leave a Reply