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.

Java 11

Java 11, like Java 10 before it, has a fairly short list of new features, which is a good thing for us developers as it’s much easier to see what may be interesting and useful to us. From an IntelliJ IDEA point of view, there’s really only one feature that benefited from some extra support in the IDE, and that was JEP 323: Local-Variable Syntax for Lambda Parameters. We’ve already blogged about this in the context of Java 11 support in IntelliJ IDEA 2018.2, but let’s cover it again quickly.

When lambda expressions were introduced in Java 8, you could write something like this:

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.

java-10-inspections

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.

hover-var

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.

quick-doc

We covered using var in quite a lot of depth in our webinar on IntelliJ IDEA and Java 10.

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.

We have already covered Java 9 and IntelliJ IDEA a number of times on this blog, and we have a recording of a webinar which covers many Java 9 features that may be interesting to developers. Of course modularity is the most famous feature, but there are lots of other additions, including the new Convenience Factory Methods for Collections. Personally this is my favorite feature from Java 9, and conveniently IntelliJ IDEA inspections can offer to migrate code to use the new methods.

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):

Java 11 may only have only just been released, but IntelliJ IDEA already fully supports it, and makes it easier to use the new features in Java 11, 10, and 9.

Try it out today! You can download the open source OpenJDK build (provided by Oracle) which is ready for production use now.

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.

This entry was posted in News & Events and tagged , , . Bookmark the permalink.

28 Responses to Java 11 and IntelliJ IDEA

  1. Yiannis says:

    Are there plans for proper OpenJFX 11 support? Especially now that JavaFX is not bundled in the JDK 11 anymore.

    • Trisha Gee says:

      I’m not sure of our plans, but I can tell you I’m doing a demo with OpenJFK next month, so I’ll be able to show what support is there and feed back any ideas for improving it.

      • Yiannis says:

        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.

  2. Jacob says:

    How does Oracle’s new license on Java 11 affect JetBrains and their users?

    • Yiannis says:

      I really don’t think it does affect us as long as we use the OpenJDK version which is totally free. Oracle’s version is based on the OpenJDK with basically commercial support and I guess some extra enterprise commercial features.
      See also here:
      https://blog.jetbrains.com/idea/2018/09/using-java-11-in-production-important-things-to-know/

      • Trisha Gee says:

        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.

        • JTW says:

          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.

  3. Joe Novak says:

    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

    Is there a tutorial for setting up a project with Javafx 11?

    • Yiannis says:

      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

      • Frank Geißler says:

        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

        • Yiannis says:

          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.

        • Trisha Gee says:

          I’m working on this problem right now. I will come back to you when I can replicate the problem.

          If you could point me to some example code that fails, that would be really helpful.

          Have you checked out these similar issues? There are some fixes and workarounds for those problems.

          • Yiannis says:

            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:

            Information:Java FX Packager: [/usr/java/jdk-11/bin/java, -Dant.home=/home/smee/Devel/ideaIU/lib/ant, -classpath, /home/smee/Devel/ideaIU/lib/ant/lib/ant.jar:/home/smee/Devel/ideaIU/lib/ant/lib/ant-launcher.jar:/usr/java/jdk-11/lib/ant-javafx.jar:/usr/java/jdk-11/jre/lib/jfxrt.jar, org.apache.tools.ant.launch.Launcher, -f, /home/smee/.IntelliJIdea2018.2/system/compile-server/testfxone_73734fd0/_temp_/build.xml]
            Information:Java FX Packager: Buildfile: /home/smee/.IntelliJIdea2018.2/system/compile-server/testfxone_73734fd0/_temp_/build.xml
            Information:Java FX Packager: [taskdef] Could not load definitions from resource com/sun/javafx/tools/ant/antlib.xml. It could not be found.
            Information:Java FX Packager:
            Information:Java FX Packager: build artifact:
            Information:javac 11 was used to compile java sources
            Information:10/18/18 12:44 AM – Compilation completed with 2 errors and 0 warnings in 3 s 528 ms
            Error:Java FX Packager:
            BUILD FAILED
            /home/smee/.IntelliJIdea2018.2/system/compile-server/testfxone_73734fd0/_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 that 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
            Error:Java FX Packager: fx:deploy task has failed.

            • Trisha Gee says:

              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.

  4. Suresh G says:

    Would be great to provide type hints for local variable types like in kotlin var/val.

  5. Thomas Prodehl says:

    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.

    • Trisha Gee says:

      I see you’ve found and commented on our issue tracker for this feature request:

      https://youtrack.jetbrains.com/issue/IDEA-171448

      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.

  6. Frank Geißler says:

    Turn on sarcasm.

    IntelliJ: Hello Community, Java 11 may have just been released, but IntelliJ IDEA already supports it completely.

    Community: Great, but if I want to build an Artifact, I get the following error: Error: Java FX Packager: BUILD FAILED …

    IntelliJ: Hello Community, Java 12 may have just been released, but IntelliJ IDEA already supports it completely.

    Community: Great, but if I want to build an Artifact, I get the following error: Error: Java FX Packager: BUILD FAILED …

    IntelliJ: Hello Community, Java 13 may have just been released, but IntelliJ IDEA already supports it completely.

    Community: Great, but if I want to build an Artifact, I get the following error: Error: Java FX Packager: BUILD FAILED …

    IntelliJ: Hello Community, Java 14 may have just been released, but IntelliJ IDEA already supports it completely.

    Community: Great, but if I want to build an Artifact, I get the following error: Error: Java FX Packager: BUILD FAILED …

    Is this the beginning of the end of Java OpenJDK? Will the InteliJ IDE be an IDE like Emabrcadero RAD Studio IDE?

    Turn off sarcasm.

  7. Techgalery says:

    I really like Java 11, the var thing is really really helpful, i am so glad to upgrade all my projects to the newest java 11, i think java will live forever, not like the rumor outside

  8. L. Holanda says:

    Hi Trisha,

    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.

    • Trisha Gee says:

      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.

Leave a Reply to Techgalery Cancel reply

Your email address will not be published. Required fields are marked *