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

Fix Gradle Eclipse task ignoring JAVA_HOME

I started playing with Java again recently and lost quite a bit of time fixing a stupid issue: when running a Gradle task from Eclipse, Gradle was ignoring JAVA_HOME environment variable. Here is how I fixed that issue.

The issue

When installing Java 8, I decided to install both the JRE and the JDK (in that order). Looks like Java now creates a symlink from C:\ProgramData\Oracle\Java\javapath to your Java bin directory and puts that as the first entry in the PATH environment variable. Doing so makes your first Java installation be your default installation.

I could not find a clean way to modify this behavior or to change the path in C:\ProgramData\Oracle\Java\javapath. Yeah, sure, I could simply modify it or remove it from the path, but without being certain that it won’t break anything I did not want to change it.

As a second issue, it looks like Eclipse ignores the Gradle wrapper (gradlew.bat on windows) or eats the environment variable. If it was using the wrapper, you’d be able to change the Java used by using the JAVA_HOME environment variable.

The fix

I decided to go the easy route and simply add my java installation into the path (as the first entry). I really don’t like this solution, it pollutes my path and prevents me from easily switching. I’ll continue to investigate why Eclipse eats the configuration and update this post (or link to a new one) once I get something cleaner.

Sources