Java 11 and IntelliJ IDEA

This week Java 11 was released! It feels like only yesterday that we was saying the same thing about Java 9. This new six monthly release cadence is a big change for the Java community, and a welcome one – Java developers are getting small drops of interesting new features regularly.

The type information is included for the x and y parameters. But you didn’t need to include this type information as it was already known – in this case, the BiConsumer on the left declares these two types with generics. IntelliJ IDEA lets you remove these types if you wish:

Java 10 introduced var for local variable types, which we’ll talk about a bit later in this post, and Java 11 took this further to allow var on lambda expression parameters. The main use case for this is when a parameter requires an annotation. Annotations appear next to the type, so prior to Java 11 this would have meant code with an annotation might look something like:

In Java 11, we can make this a little shorter using var instead of the parameter types, and IntelliJ IDEA can do this conversion for you. Note that this is suggested when you press Alt+Enter on the type, it’s not flagged as a warning in the code.

Java 11: Be aware, the APIs you use may not be there any more

As well as new language features, it’s important to understand that Java 11 actually removes features. This step not only affects deprecated features and functionality that wasn’t used much, it also aims to simplify the core of the language by moving some large sections into separate dependencies (e.g. JavaFX), or expecting applications to use external dependencies that were already available (e.g. Java EE).

Both the Java EE and CORBA modules have been removed. While CORBA is probably not highly used, many applications do, of course, make use of Java EE. Usually this is in the context of an application server, or some other specific implementation of Java EE, but some applications and libraries make use of small sections of Java EE for specific purposes. For example JAXB is now not in the core language, you’ll need to add a specific dependency on it. There’s more information on possible replacement dependencies on this StackOverflow question.

Java 10

Java 10 was released only six months ago, and many of us may not have started using it yet.

As a reminder, the main new feature from Java 10 was the introduction of var, which, as we saw above, lets us use var instead of a specific type. This is not introducing dynamic typing into Java, instead it’s continuing a trend of reducing boilerplate in Java, similar to the introduction of the diamond operator which meant we no longer had to declare generic types on both sides of the equals sign.

IntelliJ IDEA supports var in a number of ways. Firstly, inspections give you the option of replacing types with var, or var with types.

By default the inspection won’t give you a warning about code that can use var (or code that should have an explicit type), but as usual the inspection can be configured according to your team’s style.

IntelliJ IDEA can also help you to navigate code that uses var. Holding down Ctrl/⌘ and hovering over var will show the type of the variable.

Like any other type, we can click here and navigate to the declaration, or we can use Ctrl+B/⌘B to navigate to the declaration via var. We can also use Quick Documentation (Ctrl+Q/F1) or Quick Definition (Ctrl+Shift+I/⌥Space) on var to see the type.

Java 10 also came with a few nice additions to Optional and Collectors, so if you use the Streams API it’s worth having a look at these new methods.

Java 9

Last September’s Java 9 release was a big one, and people may be surprised to learn that both 10 and 11 effectively replace 9 – some JDK providers (e.g. Oracle) will not be offering long term support for Java 9 (or Java 10). Teams looking to jump straight from Java 8 to Java 11, skipping out the versions without long term support, still need to understand the changes that came in to Java 9 as obviously they’ll be part of Java 11.

A note on migration

While the goal of this post has been to show features in IntelliJ IDEA that make working with Java 9, 10, and 11 easier, and not specifically to help developers to migrate their code to these versions, we can’t help but throw in a bit of advice in this area. If you are looking to use Java 11 in the near future, you should start by making sure all of your dependencies are up to date. Many JVM languages, libraries and frameworks had to make big changes to work with Java 9, and yet more to keep up with changes from Java 10 and Java 11. You should be able to update the versions of the libraries you’re using with minimal impact on your own application and be a significant step closer to being able to use the latest version of Java.

If you are interested in migrating from Java 8, I wrote a couple of articles on the topic elsewhere, specifically tackling migrating to Java 9 (which will, of course, apply also to Java 11):

Please note that I have suggested the OpenJDK build here, as Oracle have changed their license and now produce a commercial and an open source JDK. Please do read this post for more information, it’s very important to understand.

Thank you, that sounds great! Would definitely love to take a look at it. Where do you plan to release it? It would be nice if you could provide a link.
Of course, I’ve managed to port most of the apps I had to openjdk 11 + openjfx, but I feel like it needed more configuration than I’m used to (spoiled by) on IntelliJ Idea.

I’ll be giving the demo at Oracle Code One, I’m not sure if/when any videos will be available. But I will definitely be able to write up my experiences in a blog and maybe produce a screencast as well. Will try and have something by the end of the year (no promises, this time of year is busy with conferences and releases).

Can you let me know what sort of extra support you were expecting from IntelliJ IDEA? I managed to get my JavaFX app working in IntelliJ IDEA, and I found a lot of the features I wanted worked as expected. What did you need?

Yes, this. IntelliJ IDEA runs on a JetBrains-specific OpenJDK build, and then the JDK that our users develop against is completely up to them. As per the above blog post, my recommendation would be that for Java 11 and onwards, our users should make sure they’re using an OpenJDK build and not the commercial Oracle JDK.

yeah, company I work for has decided that for now at least nobody is to install Oracle JVMs.
Legal is still trying to figure out if OpenJDK is safe for us to use without infecting our code with the GPL, which is a major concern for both us and our customers.

I am just getting into java 11. I am using openjdk and openjfx. My program compiles fine but it will not run. I get the following message:
JavaFX runtime components are missing, and are required to run this application

Edit the Run/Debug Configuration and edit the VM Options field by adding the modules you used for your project. Eg:
–module-path /home/Smeeagain/Development/Java/javafx-sdk-11/lib –add-modules=javafx.controls,javafx.fxml

I’m starting right now in java 11. I’m using OpenJDK and OpenJFX. My program compiles and starts without errors. But when I run Build Artifacts, I get the following error message.
Error: Java FX Packager:
BUILD FAILED
/Users/frankgeissler/Library/Caches/IntelliJIdea2018.2/compile-server/halloweltfx_78a3503b/_temp_/build.xml:3: Problem: failed to create task or type javafx: com.sun.javafx.tools.ant: fileset
Cause: The name is undefined.
Action: Check the spelling.
Action: Check any custom tasks / types have been declared.
Action: Check that any / declarations have taken place.
No types or tasks have been defined in this namespace yet
Total time: 0 seconds

I have the same problem as you, Frank. I tried a few things here and there, but nothing really worked. I gave up for now, hoping it’s actually a bug on Idea’s side. It was working fine before with Java 8, 9 and 10.

It’s really not about the code. I also tried the starting point of a sample JavaFX project with the exactly same results.
I simply added the static javafx library under modules -> dependencies.
The error I get when building the artifact is:

Thanks very much for creating the issue with the details in, this has been assigned to someone who should be able to help. Feel free to poke the issue or reply here if the issue isn’t moving fast enough for you.

Always there is sidestepping of IntelliJ IDEA producing jmod artifacts with jmod, or custom runtime artifacts via jlink. As of Java 11 there is no standard JRE only assemblies of reduced custom JRE’s. the JDK is a build and packaging toolset akin to Maven or Gradle which is supported. Announce or confirm interest or disinterest in support for the direction that Java is taking so that I and others can decide if paying for this product is something that we want to continue.

I can’t comment on what’s on the roadmap, but I will find out what I can. I understand it’s frustrating not having a useful feature like this (I myself wanted this a few months back when I wanted to try out the new JavaFX stuff), I’ll get back to you when I have some more information.

Every time I google for IntelliJ and Java 11, your name is in
Could you expand one of your blog posts or write a new elaborating on what is the ideal configuration for IntelliJ runtime JVM and project JVM? I see a lot around new features that IntelliJ has for Java 11 including var, lambda etc, but nothing about using Java 11 as IDE JVM.
Everytime I search on how to switch the runtime JVM, I only recommendations to stick with bundled JVM (although search results are all pre Java 11 release). I have most up-to-date IntelliJ but JVM is still based on OpenJDK 8.
I found how to force IDE JVM to OpenJDK 11. So far everything looks fine, but I want to hear if that is the recommended way.

On one side, Jetbrains recommends using bundled JVM. On the other side, doesn’t sound right run IDE on Java 8 to build Java 11 apps though.

Java 11 sounds very attractive, LTS, var keyword, and Oracle JDK vs OpenJDK interchangeability. Btw, I love your video that you dedicated 1 hour just to talk about var on Java 10, not only what is syntactically correct, but also your focus on what makes sense for code readability.

Hi, thanks a lot for your feedback, particularly the nice things you said For now, the recommendation is to stick with the bundled JVM for running IntelliJ IDEA. It appears the reasons are many and complicated (and of course we are working on getting it to work with an updated JVM), and some are to do with writing UI-specific code in Java.

There’s absolutely nothing to stop you writing your own code in Java 9, 10, 11 or even 12 in IntelliJ IDEA while the IDE runs on 8. As you’ve probably seen, I myself use much later versions than 8 without any problems.