DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • From Database Changes to Real-Time Actions at Scale: Oracle GoldenGate and Java
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop

Trending

  • Three Measured Outcomes of Manually Scaling Fleet in Prod
  • One Identity Unveils Active Roles 8.6 with Enhanced Security and Administration
  • Time to First Token: How Long Does It Take for a Large Model to Start Responding?
  • Tracing Dangerous Python Code Paths With rce-path

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. How to Export All Modules to All Modules at Runtime in Java

How to Export All Modules to All Modules at Runtime in Java

In this article, take a look at a solution as to how to export all modules to all modules at Runtime in Java 9.

By 
Jim Jerald Burton
·
Oct. 19, 21 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
10.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Due to the new module system, Java 9 does not allow an application by default to see all classes from the JDK, unlike all previous versions of Java. If we try to access some reserved module, we obtain an error like this:
module <module-name> does not "opens <package-name>" to unnamed module.

Everyone knows that we can solve this exception by using the JVM parameters --add-exports or add-opens, but what if we have more environments, and we don't want to have to change JVM arguments across these environments?

In this situation, Burningwave Core comes to our aid by providing us with the method org.burningwave.core.assembler.StaticComponentContainer.Modules.exportAllToAll() that allows us to export all modules in all modules, thus solving our problem. This method is called by default on the initialization of the class org.burningwave.core.assembler.StaticComponentContainer. This behavior can be changed by including in the base folder of your class path a file named burningwave.static.properties that contains a property named modules.export-all-to-all whose value is false, thus leaving us the possibility to call this method manually.  

Note: It is not necessary to put in the burningwave.static.properties file all the properties present in the relative link supplied before):

Java
 
Modules.exportAllToAll();
//Now that we have called exportAllToAll () we can use any class of any module
Class<?> bootClassLoaderClass = Class.forName("jdk.internal.loader.ClassLoaders$BootClassLoader");
Constructor<? extends ClassLoader> constructor =
	ClassLoader.getPlatformClassLoader().getClass().getDeclaredConstructor(bootClassLoaderClass);
constructor.setAccessible(true);
Class<?> classLoadersClass = Class.forName("jdk.internal.loader.ClassLoaders");
Method bootClassLoaderRetriever = classLoadersClass.getDeclaredMethod("bootLoader");
bootClassLoaderRetriever.setAccessible(true);
ClassLoader newBuiltinclassLoader = constructor.newInstance(bootClassLoaderRetriever.invoke(classLoadersClass));
System.out.println(newBuiltinclassLoader + " instantiated");

The Modules component also exposes other methods for greater granularity:

  • export(String moduleNameFrom, String moduleNameTo)
  • exportPackage(String moduleNameFrom, String moduleNameTo, String... packageNames)
  • exportPackageToAll(String name, String... packageNames)
  • exportPackageToAllUnnamed(String name, String... packageNames)
  • exportToAll(String name)
  • exportToAllUnnamed(String name)

Therefore, in reference to the example above, if we want to limit the export to the package of our interest, we have to replace the call exportAllToAll() with exportPackageToAllUnnamed("java.base", "java.net").

From here you can download/clone the tutorial shared on GitHub.

Java (programming language)

Published at DZone with permission of Jim Jerald Burton. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • From Database Changes to Real-Time Actions at Scale: Oracle GoldenGate and Java
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook