Search code examples
javajavacjava-9java-platform-module-system

Exporting a package from system module is not allowed with --release


I have the following program:

module-info.java

module a {
}

Main.java

public class Main {
    public static void main(String[] args) {
        System.out.println(sun.nio.ByteBuffered.class);
    }
}

This program successfully compiles with the --add-exports option:

> javac --add-exports java.base/sun.nio=a module-info.java Main.java

However, when I add the --release argument, it fails:

> javac --add-exports java.base/sun.nio=a --release 9 module-info.java Main.java
error: exporting a package from system module java.base is not allowed with --release
1 error

Basically, these two commands are equivalent. So why is the latter one forbidden?

Also, since IDEA passes the --release argument to javac, this makes the development in IDEA impossible if my project needs an internal API.

I'm using JDK 9+178.


Solution

  • Due to JDK-8178152 --release cannot be used in combination with --add-exports, --add-reads, and --patch-module.

    Quoting JDK-8178152:

    The overall proposal here is to get make --release 9 work consistently between JDK 9 and the (anticipated) JDK 10, even at the cost of a possible different behavior of commands like:
    $ <jdk9>/javac Test.java
    $ <jdk9>/javac --release 9 Test.java

    Also:

    Which should be acceptable, given the intents of --release - allow to compile code using supported APIs for the given JDK release.

    In particular the proposal is to:

    [edit]

    -prevents use of --add-exports, -add-reads and --patch-module for system modules in combination with --release (any version)

    Here is the file diff that contains:

    exporting a package from system module {0} is not allowed with --release

    I can't comment on the rationale but from the above quote, it pertains to allowing JDK 9 to work consistently with JDK 10.