20

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.

fabian
  • 80,457
  • 12
  • 86
  • 114
ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
  • @Tavo That question is about `--add-exports`. My question is about the combination of `--add-exports` and `--release` which makes it completely different. – ZhekaKozlov Jul 28 '17 at 09:55
  • As correctly said by Michael Easter, this combination of options have been forbidden in javac, so it is now for IDEA devs to adjust their compiler invocation. I've filed https://youtrack.jetbrains.com/issue/IDEA-176994 - hopefully, it will be fixed soon. – Stanislav Lukyanov Aug 02 '17 at 07:03
  • @StanislavLukyanov Thank you for the bug report! I wanted to do it but had no chance and time. – ZhekaKozlov Aug 02 '17 at 10:26
  • @StanislavLukyanov I think this is fixed in IDEA. I just tried 2017.2.5 and everything worked. – ZhekaKozlov Nov 06 '17 at 16:36

7 Answers7

11

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

xoric
  • 123
  • 1
  • 4
  • 2
    I cleared the **Use '--release' option**, IDEA doesn't add this option to the compiler and compilation is successful. – Alexey Ivanov Sep 09 '21 at 14:06
10

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.

Flow
  • 23,572
  • 15
  • 99
  • 156
Michael Easter
  • 23,733
  • 7
  • 76
  • 107
  • So, Java 10 will not allow exporting of packages from platform modules? – ZhekaKozlov Jul 31 '17 at 11:26
  • Java 10 will not allow _the combination_ of `--release` and exporting of packages from platform modules. I'm not sure that we can conclude that only `--add-exports` is not available in Java 10. That is a separate question. Please accept the answer that best answers your original question. – Michael Easter Jul 31 '17 at 21:02
  • 3
    The answer is correct. The idea of `--release` is to allow to compile against *public* API of a *specific* release, signatures of which are recorded and bundled with javac. On the other hand, `--add-exports` and its friends allow to change *private* (as well as public) API of the *current* release. To allow the combination of these options javac would need to have a record of all packages and classes of all releases. – Stanislav Lukyanov Aug 02 '17 at 07:10
  • @StanislavLukyanov I think that's the case. --release is replacing --source --target and --bootclasspath parameter which was required for cross compilation (e.g. compiling to java6 with jdk9): https://stackoverflow.com/questions/43102787/what-is-the-release-flag-in-the-java-9-compiler – MeTTeO Feb 11 '20 at 15:32
  • @StanislavLukyanov would you be open to considering moving that from a comment to an answer? Please tag me for a bounty to it once you do so. – Naman Dec 20 '20 at 13:28
9

use -target & -source instead:

-target 11 -source 11
Nino Filiu
  • 16,660
  • 11
  • 54
  • 84
William Lam
  • 91
  • 1
  • 2
  • 1
    This is not a universal solution. --release has special meaning: https://stackoverflow.com/questions/43102787/what-is-the-release-flag-in-the-java-9-compiler – MeTTeO Feb 11 '20 at 15:33
4

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

Victor Grazi
  • 15,563
  • 14
  • 61
  • 94
3

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

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
0

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.

Bob
  • 1,351
  • 11
  • 28
0

In eclipse, uncheck the --release from the project properties and not from general settings

enter image description here

Mohamed Dernoun
  • 776
  • 5
  • 13