Support for Java 9 Modules in IntelliJ IDEA 2017.1

Posted on by

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.

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

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

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.

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

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

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

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.

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.

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 .

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:

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

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.