Exporting a package from system module is not allo

2019-04-07 08:00发布

问题:

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.

回答1:

It appears from this bug ticket that --release cannot be used in combination with --add-exports, --add-reads, and --patch-module.

From the link:

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.



回答2:

If you are using IntelliJ idea, try checking your Java compiler settings:

Go to the File menu -> [Settings] -> [Build, Execution, Deployment] -> [Java Compiler] and check the setting "Project bytecode version". I got a similar error ("exporting a package from system module java.datatransfer is not allowed with --release") when using JDK 11 while this value was still set to version 9. Setting it to 11 fixed the problem for me.