0% found this document useful (0 votes)
15 views5 pages

Understanding Java 9 Module System

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Understanding Java 9 Module System

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java 9 Module System

Java Module System is a major change in Java 9 version. Java added this
feature to collect Java packages and code into a single unit called module.

In earlier versions of Java, there was no concept of module to create modular


Java applications, that why size of application increased and difficult to move
around. Even JDK itself was too heavy in size, in Java 8, [Link] file size is
around 64MB.

To deal with situation, Java 9 restructured JDK into set of modules so that
we can use only required module for our project.

Apart from JDK, Java also allows us to create our own modules so that we
can develop module based application.

The module system includes various tools and options that are given below.

o Includes various options to the Java tools javac, jlink and java where we
can specify module paths that locates to the location of module.
o Modular JAR file is introduced. This JAR contains [Link] file
in its root folder.
o JMOD format is introduced, which is a packaging format similar to JAR
except it can include native code and configuration files.
o The JDK and JRE both are reconstructed to accommodate modules. It
improves performance, security and maintainability.
o Java defines a new URI scheme for naming modules, classes and
resources.
Java 9 Modularized JDK

Java 9 Module

Module is a collection of Java programs or softwares. To describe a module, a


Java file [Link] is required. This file also known as module
descriptor and defines the following

 Module name
 What does it export
 What does it require

Module Name

It is a name of module and should follow the reverse-domain-pattern. Like


we name packages, e.g. [Link].

How to create Java module

Creating Java module required the following steps.

 Create a directory structure


 Create a module declarator
 Java source code

Create a Directory Structure

To create module, it is recommended to follow given directory structure, it is


same as reverse-domain-pattern, we do to create packages / project-structure
in Java.
Note: The name of the directory containing a module's sources should be
equal to the name of the module, e.g. [Link].

Create a file [Link], inside this file, declare a module by


using module identifier and provide module name same as the directory name
that contains it. In our case, our directory name is [Link].

ADVERTISEMENT

1. module [Link]{
2.
3. }

Leave module body empty, if it does not has any module dependency. Save
this file inside src/[Link] with [Link] name.

Java Source Code

Now, create a Java file to compile and execute module. In our example, we
have a [Link] file that contains the following code.

Save this file inside src/[Link]/com/javatpoint/ with [Link] name.


Compile Java Module

To compile the module use the following command.

After compiling, it will create a new directory that contains the following
structure.

Now, we have a compiled module that can be just run.

Run Module

To run the compiled module, use the following command.

ADVERTISEMENT

Output:

Hello from the Java module

Well, we have successfully created, compiled and executed Java module.

Look inside compiled Module Descriptor

To see the compiled module descriptor use the following command.

ADVERTISEMENT
1.

This command will show the following code to the console.

See, we created an empty module but it contains a [Link] module. Why?


Because all Java modules are linked to [Link] module and it is default
module.

Common questions

Powered by AI

Creating and executing a modular Java application involves several steps: First, define the module's directory structure following a reverse-domain naming convention. Next, create a module descriptor in the module-info.java file to declare the module's name and any exported or required elements. Then, write and organize the Java source code within the specified directories. To compile the module, use the javac command with the module path specified. Finally, execute the compiled module using the java command, ensuring that module paths are correctly set. The process encapsulates the modular elements and checks interdependencies through the module-info descriptor .

The module system in Java 9 was introduced to address the issues of managing the size and complexity of Java applications. Previously, Java applications had no modular structure, making them large and difficult to maneuver. For instance, the JDK was monolithic and heavy; for example, the rt.jar file in Java 8 was about 64MB. The modular system allows developers to create modular applications, improving performance, security, and maintainability by only using necessary modules. It also allows more efficient deployment by restructuring the JDK into a set of modules and enabling developers to define custom modules .

The Java 9 module system aims to solve several challenges from previous Java versions, notably the monolithic architecture of the JDK and runtime performance inefficiencies. Previous architecture resulted in large, cumbersome Java applications with significant dependencies, negatively affecting performance and complicating deployments. By restructuring the JDK into a modular format, Java 9 enables smaller, more targeted runtime images, improving startup times and memory usage. This also reduces the overall attack surface for security vulnerabilities and improves maintainability by decoupling code modules, enabling easier management and updates .

The introduction of modular JAR files in Java 9 significantly impacts Java application scalability and maintainability by allowing developers to package code cleanly with defined module boundaries. This promotes clear separation of concerns and code organization, making scalable application design more feasible. The modular JAR files include a module-info.class to define dependencies and exports, providing clarity and easing maintenance as system complexity grows. This modular approach makes large projects more manageable by enabling independent module upgrades and encourages better architecture practices, fostering scalable growth within extensive codebases .

The module descriptor in Java 9's module system is a crucial component defined in the module-info.java file. It specifies the module's name, what it exports, and what it requires. The module name should follow the reverse-domain-pattern to avoid naming conflicts. The descriptor allows developers to declare dependencies between modules explicitly, enabling the Java runtime to optimize resource loading and module access control efficiently .

The new URI scheme introduced in Java 9 aids in managing modules and resources by providing a standardized way to refer to module components such as classes and resources, similarly to how URL members are resolved. This new systematization ensures consistent access methods regardless of where or how the code is deployed, thereby enhancing the reliability and efficiency of resource management across modularized applications. This approach aligns well with modularization's core principles of encapsulation, transparency, and manageability, thus also streamlining interactions within module ecosystems .

The java.base module is integral to all Java modules created in Java 9 because it serves as the foundational module providing essential classes and infrastructure. It is implicitly required and linked to every other module, meaning that each module automatically has access to base Java APIs such as java.lang and java.io. This design ensures that all modules have a minimum necessary environment to function, allowing developers to focus on dependencies specific to their application without redundantly including the basic APIs. It simplifies module creation and ensures uniform access to core Java functionalities .

The Java 9 module system enhances security by encapsulating packages within modules, which restricts access to internal API, a common source of security vulnerabilities. By using modules, developers can explicitly declare which parts of their code are public and accessible to other modules and which parts are internal, thus reducing the risk of unintended access or usage of classes and methods. This enhances the security posture as only necessary code is exposed, minimizing potential attack vectors .

The `jlink` tool in the Java 9 module system provides significant benefits, including the ability to create custom Java runtime images containing only the required modules. Compared to previous Java build tools, `jlink` offers improved application performance and reduced footprint by eliminating unnecessary components from the runtime, thus producing smaller, optimized distributions. Additionally, `jlink` ensures that runtime images are modular, facilitating better application deployment and ensuring compliance with specific use-case requirements, enhancing both development and operational efficiencies not seen with earlier tools .

The JMOD format introduced in Java 9 differs from the traditional JAR format in several ways. While both are used to package Java code, JMOD can include not only .class files but also native code and configuration files, enhancing its utility for modular applications that require additional resources beyond Java binaries. This capability makes JMOD more flexible for creating complex modules and ensures all files a module might need are bundled efficiently. However, JMOD is not intended for distribution as it is not a replacement for JAR files which are still used for internet and network-distributed components .

You might also like