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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • How to Introduce a New API Quickly Using Micronaut
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Trending

  • Navigating Change Management: A Guide for Engineers
  • How to Introduce a New API Quickly Using Micronaut
  • Memory-Optimized Tables: Implementation Strategies for SQL Server
  • Simpler Data Transfer Objects With Java Records
  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 user avatar
Jim Jerald Burton
·
Oct. 19, 21 · Code Snippet
Likes (11)
Comment
Save
Tweet
Share
9.8K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • How to Introduce a New API Quickly Using Micronaut
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!