Features

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.

image description