A Java module is a self-contained, organisational unit of code and resources within an application, encapsulating a specific set of functionalities beyond what is possible with regular access modifiers.
We normally use the package name for consistency, clarity and avoiding conflicts.
Notice how we’re able to specify which packages we do/don’t want to expose.
By default packages are inaccessible, we specify the ones we wanna export.
Until now it was never possible to dictate accessibility and groupings in packages, as you can’t write code or access modifiers on a package or group of packages directly.
All public types (classes, interfaces, fields, methods etc.) are freely accessible by all code in the same application.
This leads to inadvertent dependencies on code that the developers never intended for public use.
So, when the developers change this code, our code breaks.
Lacking encapsulation (a clear distinction between “Yes, this is for public use, go ahead” and “Nah, this is for internal use only”) leads to reduced maintainability.
The JPMS allows developers to create modules by attaching meta-information about a group of packages, making JARs more than just containers.
This metadata states the developer’s intent for the module’s identity, purpose and relationship with other modules.
Defines its own namespace, isolating its components from the rest of the application.
Specifies its dependencies explicitly, ensuring a clear and manageable architecture.
Encapsulates its internal implementation details, controlling access to its API.
Provides versioning and encapsulation boundaries, promoting long-term stability.
Using modules, both the developers and Java (during compilation and runtime) understand the identity, purpose and relationships between modules, and we can thus solve problems like missing or duplicate dependencies more easily.
Advantages of Using Modules
Better access control
With public, protected, package private and private, we can limit and control access to a type.
What if we want to limit access when it comes to packages? What if we only want to expose a select amount of our packages with fine-grained control?
Modules provide a higher level of access control.
With the module descriptor (module-info.java-file) we specify which packages we want to expose for stronger encapsulation, we declare our dependencies, and name our module.
This allows us to differentiate between private packages for internal use, and public packages intended for external use.
Better code quality
Encourages us to think modularly.
To be conscious of logical organisation of our code.
We can now explicitly specify our module’s dependencies in the module descriptor.
This makes it not only clear to us developers, but also lets Java help us during compilation and runtime to track down and fix any dependency issues, because now Java knows what our intentional dependencies are.
Lighter JRE
JDK is > 150MB.
By specifying the necessary JDK-modules, and by using the jlink-tool, developers can have their programs run on a lighter version of the JRE.
This also has the added side-effect benefit of increased security: if there’s a security vulnerability in one of the modules, and your application doesn’t use that module, you won’t be affected.
Better performance
Java knows which modules it needs now thanks to JPMS.
The JVM will only search for those modules during class-loading.
Improves startup-time.
Reduces memory footprint.
More info on this topic can be found on the JSR 376.
The Module Path
Introduced alongside the Java Platform Module System in Java 9.
Modern alternative to the traditional classpath, designed specifically to work with modules.
A way of specifying the location of Java modules to the Java runtime, similar to the classpath, except modules are defined in a structured manner with defined dependencies and exports.
Module Path vs. Classpath
Feature
Classpath
Module Path
Organization
Unstructured, flat. All classes and packages are accessible indiscriminately.
Structured and hierarchical. Modules explicitly define dependencies and accessible packages.
Dependency Handling
Manual management, prone to errors and conflicts such as “JAR hell”.
Dependencies are explicitly declared and managed, reducing conflicts and ensuring more reliable configurations.
Visibility
All packages are indiscriminately visible to the class loader, potentially exposing internal APIs.
Strong encapsulation by default. Only explicitly exported packages are visible outside the module.
Usage
Used with traditional Java applications.
Recommended for new applications to leverage modularity for better encapsulation and dependency management.
Interoperability
Only operates within the traditional loading mechanism, without awareness of module boundaries.
Can coexist with classpath to support hybrid applications during transition. Modules can explicitly specify allowed reflective access.
Example: Setting the Module Path
When compiling and running Java applications, the --module-path (abbreviated to -p) option is used to specify the path to modules. Here’s how you might compile and run a modular application:
Compilation:
javac -p lib -d out --module-source-path src/com/example/MyApp/module-info.java src/com/example/MyApp/MyApp.java
Running:
java -p out --module com.example.myapp/com.example.myapp.MyApp
We gebruiken cookies op onze website om u de meest relevante ervaring te bieden door uw voorkeuren en herhaalde bezoeken te onthouden. Door op "Alles accepteren" te klikken, stemt u in met het gebruik van ALLE cookies. U kunt echter "Cookie-instellingen" bezoeken om een gecontroleerde toestemming te geven.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duur
Beschrijving
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.