Example example_addExports_manifest

Part of the full Java 9 Jigsaw modules example suite.

Authors

Migrated for Java Modules support documentation of Apache MavenTM in the course of the Maven Support & Care program by Gerd Aschemann (and other team members) as forked repository. Please add discussions, requirements, bugfixes, etc. to the fork instead of the original.

What is this example about?

Modules in this example

  • modmain

  • modmain has a Main class which is started in run.sh

Module Dependency Graph, created with DepVis

Example’s Module Dependency Graph

Example shows …​

how to use --add-exports in a manifest file for the Java launcher.

We use Add-Exports in the manifest file of modmain to gain access to internal java.base classes (without a command line option)

For this to work

  • modmain has to be started as an executable jar

  • Add-Exports must be specified in the manifest file of modmain

  • modmain has to be in the unnamed module

Don’t try this at home

This example demonstrates an anti-pattern. Using --add-reads and --add-exports in production/main code is strongly discouraged.

These flags should only be used for testing scenarios where you need test-only dependencies (like JUnit) without polluting production module descriptors.

For a realistic example of proper usage, see Issue #5: Realistic testing example.

Better alternatives: Declare proper requires and exports in your module-info.java files instead.

Output

This example uses golden master testing to ensure output consistency. The expected output is compared with actual output using verify.sh.

Expected Output

Do you want to know a secret: jdk.internal.misc.SharedSecrets
from A
Do you want to know a secret: jdk.internal.misc.SharedSecrets
from A

Actual Output

Do you want to know a secret: jdk.internal.misc.SharedSecrets
from A
Do you want to know a secret: jdk.internal.misc.SharedSecrets
from A

Maven 4 Output

Do you want to know a secret: jdk.internal.misc.SharedSecrets
from A
Do you want to know a secret: jdk.internal.misc.SharedSecrets
from A