Skip to content
Domain Specific Language

Java 8 to 11: TLDR edition

  1. Updates
  2. Why now?
  3. Prerequisites
  4. Relevant documentation
  5. Why OpenJDK?
  6. List available Java versions
  7. Getting Java 11 installed
  8. Update to use Gradle 5.0
  9. Java Modules / Project Jigsaw
  10. IntelliJ IDEA support
  11. What did I get?
  12. Error Prone
  13. Combining Gradle, Error Prone, and Lombok

It's been two years and two months since I last wrote a blog post - I promise to make it count!

Updates

Why now?

Prerequisites

If you skip the parts about installing Java 11 it's mostly platform independent after that.

Relevant documentation

If you don't want to read the rest of my incoherent ramblings:

Why OpenJDK?

As mentioned in JetBrains: Using Java 11 In Production: Important Things To Know (by Trisha Gee!)

Oracle’s JDK (commercial) – you can use this in development and testing for free, but if you use it in production you have to pay for it.

You probably don't want to pay for it.

Even better:

Note that since Java 11, Oracle’s commercial JDK and Oracle’s OpenJDK builds are functionally the same, so we should be able to run our applications on either without having to make any changes or losing any features.

List available Java versions

/usr/libexec/java_home -V

On my machine this currently yields

% /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    1.8.0_144, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home

    /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home

Getting Java 11 installed

brew tap AdoptOpenJDK/openjdk
brew cask install adoptopenjdk11

And now let's check what happened

% /usr/libexec/java_home -V

Matching Java Virtual Machines (2):
    11.0.1, x86_64:	"AdoptOpenJDK 11.0.1"	/Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.1.jdk/Contents/Home
    1.8.0_144, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.1.jdk/Contents/Home

Magic!

Now if I wanted to, I could uninstall Java 8, probably. But I don't. You might though.

If you want to, please do:

Update to use Gradle 5.0

Gradle 5+ is required for Java 11. Do your upgrade in whatever way you usually do it. Personally I add a configuration to my root build.gradle file that looks like this:

wrapper {
    gradleVersion = '5.0'
    distributionType = Wrapper.DistributionType.ALL
}

And then just run the associated task ./gradlew wrapper, which updates Gradle for that project.

Java Modules / Project Jigsaw

No need to use Java Modules if you don't want or need them!

IntelliJ IDEA support

Add and set the JDK

First add Java 11 as a JDK - go to File -> Project Structure -> Project. Click New next to the Project SDK dropdown, (if Java 11 is not already present in the dropdown list). Now find the install path of Java 11. Should be the path that was output from the above java_home command: /Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.1.jdk/Contents/Home.

When switching the project between JDK 8 and JDK 11, which might happen when switching branches or fixing bugs based on older commits, you may need to visit the Project Structure dialog again.

Set JDK/JRE for the Gradle runtime

This is important since for some reason the project JDK isn't the same as the Gradle JDK/JRE.

Open the Gradle sidebar and click on the wrench, or go to Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle. There you will find a box called Gradle JVM where you can select which version of Java that gradle should run as when IntelliJ needs to run it.

This is important if you use the IntelliJ builder for compiling, but delegate to Gradle when testing.

Use a Gradle configuration to set JDK version for IntelliJ subprojects

Go to the root build.gradle and enter this nugget somewhere:

allprojects { project ->
    project.plugins.withId('java') {
        sourceCompatibility = 11.0
        targetCompatibility = 11.0
    }
    project.plugins.withId('java-library') {
        sourceCompatibility = 11.0
        targetCompatibility = 11.0
    }
}

Now refresh the Gradle project in IntelliJ and you should be able to use Java 11 everywhere, since IntelliJ picks up that subprojects are Java 11.

What did I get?

Wikipedia has got your back, but here's the TLDR:

But mostly you got the freedom and/or burden to adopt new Java versions faster!

Error Prone

You can now use the latest version of Error Prone, which should make you very happy.

Combining Gradle, Error Prone, and Lombok

To get Gradle 5.0 to accept Lombok using Java 11 in combination with Error Prone I had to change the following

From

dependencies {
    compileOnly "org.projectlombok:lombok"
    testCompileOnly "org.projectlombok:lombok"
    
    // ... more dependencies
}

To

dependencies {
    compileOnly "org.projectlombok:lombok"
    annotationProcessor "org.projectlombok:lombok"
    testCompileOnly "org.projectlombok:lombok"
    testAnnotationProcessor "org.projectlombok:lombok"
    
    // ... more dependencies
}

That way, Error Prone knows about the code generated by Lombok.

Please note that I define dependency versions globally using the Spring Dependency Management Plugin.