Support for Java 9 Modules in IntelliJ IDEA 2017.1

Trisha Gee

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.

Comments below can no longer be edited.

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

  1. Jeff says:

    March 21, 2017

    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.

    • Trisha Gee says:

      March 21, 2017

      Agreed, and yes it is the plan to make this as simple as possible and automate as much as possible

  2. Lukas M. says:

    March 22, 2017

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

    • Trisha Gee says:

      March 27, 2017

      I haven’t tried it with Maven yet. I know Gradle multi-module projects don’t work at the moment because of the changes to the classfile format in one of the earlier Java 9 EAs. Both Maven and Gradle are working towards Java 9 compatibility though.

      It looks like this might be the page that tracks the status of Maven’s Java 9 support:

      https://cwiki.apache.org/confluence/display/MAVEN/Java+9+-+Jigsaw

  3. Adelle says:

    March 22, 2017

    Perfect! It is a big help!

  4. AbuAbdullah says:

    March 23, 2017

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

    • Trisha Gee says:

      March 27, 2017

      Support for JLink is on the roadmap:

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

      But it hasn’t been implemented yet. Feel free to upvote if you’re going to need it, and/or add comments about how you might want to use it.

  5. Ed says:

    March 25, 2017

    Will this also work with Scala projects?

    • Trisha Gee says:

      March 27, 2017

      It doesn’t look like Scala supports Java 9 yet:

      https://github.com/scala/scala-dev/issues/139

      Java 9 has a LOT of changes under the covers, which impacts pretty much every library and JVM language. They’re all working hard to support Java 9, I expect they’ll welcome any feedback or suggestions.

  6. Henning says:

    March 25, 2017

    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:

      March 27, 2017

      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:

    April 11, 2017

    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. Christian Stein says:

    April 15, 2017

    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:

    April 27, 2017

    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

  10. Avo H says:

    May 5, 2017

    Hi there!
    I have 2017.1.2 build from Apr 25th and java 9ea(build 9-ea+168).
    I went a head and created a new project and a Hello class.
    All compiles and works well. However, as soon as I create the module-invo.java file (using New->module-info.java menu, the compiler starts complaining:

    C:\Users\me\IdeaProjects\hellojava\one\src\com\xyz\hello\HelloJava.java
    Error:(1, 1) java: file should be on source path, or on patch path for module
    C:\Users\me\IdeaProjects\hellojava\one\src\module-info.java
    Error:(4, 1) java: file should be on source path, or on patch path for module

    Am I missing any configuration steps?

    • Trisha Gee says:

      May 8, 2017

      Unfortunately this is a new problem in IntelliJ IDEA / Java 9, in fact it’s one I ran into myself just this morning. IntelliJ is only going to work with 9-ea+166 (or lower) at the moment

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

      There is a fix in place but it’s not out yet, it will be coming soon.

  11. Ash says:

    May 22, 2017

    Great work on supporting the Java Module System. I’ve noticed that it’s not possible to edit the module-path in the Run Configurations. This would be really useful, for instance, if wanting to include various service providers for different configurations. As it stands, the only way I’ve found to achieve this is by adjusting module dependencies each time, which is really cumbersome and seems kind of clunky. Another workaround is explained here: https://medium.com/@andrey_cheptsov/using-jdk-9-with-project-jigsaw-in-intellij-idea-95e495c825aa. Is the ability to edit the module-path in Run Configurations a feature you are planning to add, perhaps in the 2017.2 EAP?

    • Trisha Gee says:

      May 23, 2017

      It doesn’t look like we have a request for this feature (yet). The closest I’ve found is a request for more control over what goes onto the module path and what goes onto the classpath (IDEA-171320). If this doesn’t cover what you want, please do raise a ticket in YouTrack with details of your use case and we can add it to the backlog.

  12. Abhijit Sarkar says:

    August 28, 2017

    I’ve a learning project with the following structure:

    math-tutor
    ├── addition-provider
    │   └── src
    │   ├── module-info.java
    │   └── org
    │   …
    └── tutor
    └── src
    ├── module-info.java
    └── org

    When I open up math-tutor in IntelliJ 2017.2.3 CE, and go to Project Settings -> Modules, I don’t see tutor or addition-provider. Do I need to manually add them? If so, how (there’s no option to add an existing Java 9 module, just to Add new module)?

    • Trisha Gee says:

      August 28, 2017

      You need to manually add Java 9 modules as IntelliJ IDEA modules. This step will be removed in time, but for now you need to do this yourself.

  13. Abhijit Sarkar says:

    August 28, 2017

    Sorry, above the files and folders shown below tutor are inside it, not at the same level. Basically, math-tutor only has 2 subdirectories, addition-provider and tutor.

    P.S.: You should consider having an Edit comment option.

  14. Dkyc says:

    September 11, 2017

    I just don’t understand why on Earth are we using a .java file for module configuration as opposed to an XML or even JSON?

    • Trisha Gee says:

      September 12, 2017

      I can’t find detailed information about this, but I’m assuming it’s something to do with the JVM/compiler being able to understand .java/.class files much better than trying to parse an XML or JSON file.

      This blog post, for example, states: “… the module-info.java file contains a binding part of your API, and that is best expressed in .java code, not metadata like pom.xml.”

    • Trisha Gee says:

      September 13, 2017

      More detailed information is here:

      http://mail.openjdk.java.net/pipermail/jpms-spec-observers/2015-October/000165.html

  15. Denis says:

    September 28, 2017

    I am a bit confused, according to this http://openjdk.java.net/projects/jigsaw/quick-start guide module-info placement supposed to be under the same src folder:
    src/org.astro/module-info.java
    src/org.astro/org/astro/World.java
    src/com.greetings/com/greetings/Main.java
    src/com.greetings/module-info.java
    But IntelliJ forces to use separate src folder per module e.g:
    org.astro/src/module-info.java
    org.astro/src/org/astro/World.java
    com.greetings/src/com/greetings/Main.java
    com.greetings/src/module-info.java
    and each of them must be configured as a module.
    Why it’s so?

    • Trisha Gee says:

      September 29, 2017

      IntelliJ IDEA doesn’t force the different structure at all. All you need is to declare the folder “org.astro” or “com.greetings” as the sources root, instead of having a src folder.

      Yes, when you create a new project in IntelliJ IDEA it generally creates a src folder which is configured as the root, but this is not mandatory.

      It is important to note that your Java 9 module (com.greetings, org.astro) does need to also be an IntelliJ module. But this is the same as if you were using modules/subprojects in Maven or Gradle.

  16. mading says:

    October 19, 2017

    why shuold I manage two module frameworks IntelliJ module and jigsaw in parallel?
    hope naked jigsaw project works out of the box one day.

  17. EliIJ says:

    April 10, 2018

    Great article! Useful to reduce my ignorance on java 9 module logic, thanks!

  18. Looking into Java 9 – Mikael Nyvelius says:

    October 10, 2018

    […] using IntelliJ, which has a guide to Java 9 modules in Intellij on their blog. Following that guide, let’s create an empty […]

  19. Ewa says:

    May 17, 2020

    I created a project with Maven support. For some reason (probably because it’s not marked as source directory) when I right-click on the main directory, I don’t have this “add module-info” option.

    I know I can create a file manually, but it’s not very convenient. Am I missing something here?

  20. Ewa says:

    May 17, 2020

    Ok, I figured it out, so I’ll respond myself.
    First thing: module-info should be under src directory, not under the main one.
    Further, when I you Maven project, it has by default some old version of Java set (not sure why), and that’s the reason the option no add it is not there. When I set Java version to 9 in maven-compiler-plugin in pom.xml (that may be done automatically after you create module-info manually and it doesn’t work – Idea propose this solution and does it for you), the option appears there.

Subscribe

Subscribe for updates