Java 8 to 11: TLDR edition
It's been two years and two months since I last wrote a blog post - I promise to make it count!
Updates
- 2019-01-05 Added section on combining Gradle, Error Prone, and Lombok.
- 2019-01-05 Clarified which JNLP image to use.
- 2019-01-18 Added section on JDK/JRE/JVM switching in a IntelliJ project that uses Gradle.
Why now?
- The Jenkins Docker JNLP Slave image finally has a version that supports Java 11!
- Use
FROM jenkins/jnlp-slave:latest-jdk11
for now. - Issue tracker
- Docker Hub
- GitHub
- Use
- Many projects have recently solved their initial Java 9 issues (which are required for Java 11 support)
- Including Spring!
- I am no longer procrastinating.
- Maybe you thought the title would explain why I stopped blogging.
Prerequisites
- macOS 10.14
- Homebrew
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
On my machine this currently yields
)
Getting Java 11 installed
And now let's check what happened
)
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
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
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:
- Java 9
G1 Garbage Collector
is now default- Not much else,
Java Modules
is probably the only thing of developer interest and you don't need to use it unless you are writing a library
- Java 10
var
keyword and Local-variable type inference- The rest is mostly runtime and behind-the-scenes stuff
- Java 11
- Local-Variable Syntax for Lambda Parameters, more
var
:)
- Local-Variable Syntax for Lambda Parameters, more
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
To
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.