cannot compile Java 9 module with --patch-module in IntelliJ IDEA 2017.2.1
Asked Answered
M

1

22

I am trying to acquaint myself with Java 9 modules and how to define them in IntelliJ. Among other things, I want to solve a split package problem using the --patch-module compiler/JVM flag and I don't know how to make it work in IntelliJ.

I am using IntelliJ IDEA 2017.2.1 Build #IC 172.3544.35 with Java HotSpot(TM) 64-Bit Server VM (build 9+180, mixed mode).

This is my source file MyImmutableList.java:

package com.google.common.collect;

public class MyImmutableList extends RegularImmutableList {
  public MyImmutableList(Object[] array) {
    super(array);     
  }
}

It belongs to my module com.effjava.collect with module-info.java:

module com.effjava.collect {
    // do not require module guava; instead patch this module with guava.19-0.jar via:
    // javac --patch-module com.effjava.collect=guava-19.0.jar module-info.java com/google/common/collect/MyImmutableList.java
    //    requires guava;        
    exports com.google.common.collect;
}

For the compilation of my modules, I specified the --patch-module flag in IntelliJ using Settings => Build,Execution,Deplyoment => Compiler => Shared build process VM options as described here.

The compilation fails because the compiler cannot find the superclass RegularImmutableList from the guava library.

The advice provided by IntelliJ is to add a requires directive for the guava library to the module descriptor, which of course does not work because the compiler then rightly complains about the split package problem:

Error:java: module guava reads package com.google.common.collect from both com.effjava.collect and guava

On further investigation, I noticed that the build process issues a warning related to the --patch-module flag saying:

Error:Abnormal build process termination: "C:\Program Files\Java\jdk-9\bin\java" -Xmx700m -Djava.awt.headless=true

... lots of stuff ...

WARNING: Unknown module: com.effjava.collect specified to --patch-module

... more stuff ...

BTW, I did not know how to get hold of the compiler warnings. I do not hide them in IntelliJ's message window and yet I cannot see them. As a workaround, I simply specified bogus compiler flags and let the build process crash so that it emitted the WARNING in its crash report.

Anyway, the build process complains - while building the module - that the module in question does not exist and for this reason cannot be patched via --patch-module. This complaint does not make sense because I did successfully compile and build my module by typing in the javac and jarcommands manually on the command line level:

cd %PROJECTDIR%\com.effjava.collect\src
javac --patch-module com.effjava.collect=../../guava/guava-19.0.jar -d ../../out/production/com.effjava.collect module-info.java com/google/common/collect/MyImmutableList.java
jar --create --file=../../out/artifacts/com_effjava_collect_jar/com.effjava.collect.jar -C ../../out/production/com.effjava.collect .

How can I tell IntelliJ's build process that I want to patch my module?

Merited answered 7/8, 2017 at 11:21 Comment(3)
Am I understanding you correctly that you're trying to patch the module under compilation? If so, I'd guess that that's the problem.Elishaelision
Why would it be a problem to patch the module under compilation? It works when I type in the javac command by hand. In fact, I have a batch file that compiles all my modules with the --patch-module flag, creates all the modular jars, and eventually runs the main module with the --patch-module flag. The problem is just with IntelliJ. How can I tell IntelliJ that I want to patch a module?Merited
Oops, didn't see that. Well, then I'm out of guesses. ;)Elishaelision
S
2

Well eventually with the following configuration of the IntelliJ(Fix version of IDEA-169150) -

IntelliJ IDEA 2018.1 (Community Edition)
Build #IC-181.4203.550, built on March 27, 2018
JRE: 1.8.0_152-release-1136-b20 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.3

and the Java Compiler settings for the project as the one in the following screenshot:-

Java compiler configuration

  • Override compiler paramaters per-module
  • Select the module
  • Add the compilation option to patch (Note - I've used the latest version of the jar)

    --patch-module com.effjava.collect=/Users/naman.nigam/.m2/repository/com/google/guava/guava/24.1-jre/guava-24.1-jre.jar
    

Post this select Build > Build Project and the build completes successfully(though I still see RegularImmutableList in MyImmutableList as red) for me.

enter image description here

Sable answered 2/4, 2018 at 16:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.