Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between --add-exports and --add-opens in Java 9?

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.

like image 299
vip Avatar asked May 18 '17 19:05

vip


People also ask

What is ADD Opens Java?

--add-opensSome libraries do deep reflection, meaning setAccessible(true) , so they can access all members, including private ones. You can grant this access using the --add-opens option on the java command line. No warning messages are generated as a result of using this option.

Which of the following is correct about module system in Java 9?

Q 7 - Which of the following is correct about Module System in Java 9? A - javac, jlink, and java have additional options to specify module paths, which further locate definitions of modules.

What are modules in Java 9?

Defining the Java 9 module. A module is a collection of code, data, and resources. It is a set of related packages and types (classes, abstract classes, interfaces, and more) with code, data files, and some static resources. For example, the module descriptor module-info.

How do modules work in Java?

A Java module is a packaging mechanism that enables you to package a Java application or Java API as a separate Java module. A Java module is packaged as a modular JAR file. A Java module can specify which of the Java packages it contains that should be visible to other Java modules which uses this module.


1 Answers

  • 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).

like image 91
Nicolai Parlog Avatar answered Sep 21 '22 09:09

Nicolai Parlog