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

Viewed 17035

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.

6 Answers

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.

Screenshot

use -target & -source instead:

-target 11 -source 11

Go to the File menu -> [Settings] -> [Build, Execution, Deployment] -> [Java Compiler] and select your JDK version. (Make sure it is higher than JDK 11)

See the image below

If you are using IntelliJ, there is a better solution. Just uncheck the "Use '--release' option" checkbox in the Java compiler settings enter image description here

If you are using InetlliJ Idea do this setting: Go to the File menu -> [Settings] -> [Build, Execution, Deployment] -> [Java Compiler] and select your JDK version and sometimes this is empty so insert the java version like [if you use 11] and save, Then compile the program and run.

Related