100

Java 9 (jdk-9+170) does not allow by default an application to see all classes from the JDK, unlike all previous versions of Java, due to the new module system.

To workaround this, the java command line offers a new argument --add-exports which allows to break encapsulation as follows:

java -jar josm.jar --add-exports java.base/sun.security.util=ALL-UNNAMED --add-exports java.base/sun.security.x509=ALL-UNNAMED

This is well explained in JEP 261.

I have read about a similar option --add-opens using the same syntax, but the JEP 261 has not yet been updated to describe it (last update: 2017/03/08 13:58).

What is the difference between these two options?

EDIT: The JEP 261 has been updated on 2017-09-22 to explain it.

Naman
  • 27,789
  • 26
  • 218
  • 353
vip
  • 1,707
  • 2
  • 16
  • 46

1 Answers1

169
  • With --add-exports the package is exported, meaning all public types and members therein are accessible at compile and run time.
  • With --add-opens the package is opened, meaning all types and members (not only public ones!) therein are accessible at run time.

So the main difference at run time is that --add-opens allows "deep reflection", meaning access of non-public members. You can typically identify this kind of access by the reflecting code making calls to setAccessible(true).

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Nicolai Parlog
  • 47,972
  • 24
  • 125
  • 255
  • 3
    The two bullets describe that the types/members made accessible by `--add-opens` are a superset of `--add-exports`, so I don't think saying one implicates the other adds any value to the description. – Nicolai Parlog May 22 '17 at 06:30
  • 1
    This is now explained in JEP 261 as Mark updated the page today: http://openjdk.java.net/jeps/261 – vip Sep 22 '17 at 21:27
  • 2
    A command to find which module provides which package: `java --list-modules | tr @ " " | awk '{ print $1 }' | xargs -n1 java -d` the name of the module will be shown with the @ while the name of the packages without it – Carlos Saltos Jun 28 '20 at 23:16
  • it is not clear from the answer and @ZhekaKozlov combined if --add-opens enables public types and members to be accessible at **compile time** – Line Oct 30 '20 at 20:08
  • 1
    `--add-opens` is only concerned with reflection, which is a run-time concept, and so the flag does not apply to the compiler. – Nicolai Parlog Nov 01 '20 at 15:40