Support for Java 9 Modules in IntelliJ IDEA 2017.1

IntelliJ IDEA 2017.1 brings support for Java 9’s new modularity (Project Jigsaw). In this latest version, support includes code completion in the module-info.java file, and quick fixes for adding modules to your project.

Let’s take a look at a basic example of a modular project. To start with, we just have an ordinary IntelliJ IDEA module that contains a simple “Hello World” type application.

A simple application

IntelliJ IDEA lets you create a new module-info.java for your module.

Create new module-info.java

This module-info.java will have the basic structure provided for you.

Default module-info.java

Everything continues to work as expected with this file in place, for example running the application works normally. Where you start to see the impact of using modularity is when you try to use Java classes that you’d normally be able to access.

Error using Logger

Here, IntelliJ IDEA can help you to figure out what the problem is, and offer suggestions to fix it.

Add requires to module-info.java

As you’d expect, IntelliJ IDEA makes the correct changes to the module-info.java file.

Requires added

Of course, you can edit the module-info.java file yourself, and the IDE offers code completion and suggestions.

Code completion in module-info.java

Quick fixes are not only available for the standard Java modules, but also to help you write modular code yourself.  If you’re trying to access code that belongs in another IntelliJ IDEA module, from inside a module that uses Java 9’s modularity (indicated by the presence of a module-info.java file), IntelliJ IDEA will show you this isn’t going to work without some more changes.

Using other modules

Firstly, there’s more code completion available to help you make the changes to the module you need to access. So once you’ve got a module-info.java file in the module you want to use, you can quickly export the required packages.

Code completion for exports

Back in the module that needs to use this newly exported code, using Alt and Enter gives suggestions as to how to fix the problems. There are two steps in this case: firstly, the IntelliJ IDEA module one needs to have a dependency on the IntelliJ IDEA module two.  Once this is done, then the module-info.java file for module one can be updated to show that the module requires module two.

Quick fixes for using modules

It’s important to note that there are two systems of modularly here: firstly, the IntelliJ IDEA modules that you’re probably already familiar with; and secondly, the new Java 9 (Jigsaw) modules that are configured using module-info.java.  To use Java 9 modularity, each Java 9 module needs to correspond to an IntelliJ IDEA module. Also note that (as shown in the last example) in IntelliJ IDEA 2017.1 you need to declare your IntelliJ IDEA module dependencies as well as your Java 9 module dependencies. So in the last example, module one was dependent upon module two:

IntelliJ IDEA module dependencies

But it also needed to declare that it requires module two in module-info.java.

Module-info dependency

In summary, although Java 9 isn’t expected just yet, IntelliJ IDEA 2017.1 provides support for working with Java 9 modularity (Jigsaw), with code completion, quick fixes and help for using the new module system.

If you want to start working with Jigsaw, check out the OpenJDK Quick Start guide, download IntelliJ IDEA 2017.1 and take a look at our Getting Started with Java 9 tutorial.

This entry was posted in New Features and tagged , , , . Bookmark the permalink.

15 Responses to Support for Java 9 Modules in IntelliJ IDEA 2017.1

  1. Jeff says:

    Looks like a pain to keep the java module and intellij module dependencies in sync, I sure hope this is done automatically in the future.

  2. Lukas M. says:

    What about relationship with maven and multi-module maven project. It’s compatibile with Java 9 modules?

  3. Adelle says:

    Perfect! It is a big help!

  4. AbuAbdullah says:

    what about linking the app with only the necessary parts of the JDKs (jlink)

  5. Ed says:

    Will this also work with Scala projects?

  6. Henning says:

    Thank you for this posting – it’s PERFECT!

    It’s timely, it’s short and to the point and it’s not a video (yet the demo parts are animated) – it even gives just enough background that I can understand the feature with almost no prior knowledge of Jigsaw.

    • Trisha Gee says:

      Thank you! I’m very glad it was helpful, I was aiming to create a demo that gives developers a flavour of the functionality without needing a deep-dive on all the details.

  7. Anton says:

    Thank you for demo and reassurance! Everyone hypes a lot but few show how exactly it affects our development. This is invaluable. Of course as you said we will eagerly wait for build tools to support Java 9 modularity.

  8. Hi Trisha,

    I just published a proof-of-concept project that demonstrates IDEA’s support of Java 9 modules and testing a modular project with “JUnit 5” standalone distribution.

    https://github.com/sormuras/application-junit5-jdk9-demo

    What do think about the project layout? What can be improved?

  9. Torsten says:

    Thank you for demo.
    But when I try to add JUnit-Tests, I get either:
    Error: java: package org.junit is not visible
    (package org.junit is declared in module junit, but module does not read it)
    or I get:
    Error: java: module not found: junit

Leave a Reply

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