search
HomeJavajavaTutorialJava 9 Modules

Java 9 Module is the new entity that is introduced as a collection of packages to provide a certain feature or functionality. Java 9 module is the first release of the java development kit after the rearrangement and categorization of packages into modules. It is also known as Java 9 Platform Module System (JPMS). In java 9, you can reduce the runtime size by including only the required modules. For example, for a device that does not support GUIs, you can create a runtime without including the GUI modules.

Java 9 Modules

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

 

Key Highlights

  • Java 9 module is the implementation of a high-level abstraction of packages.
  • It has all packages related to functionality packed into one module with a total of 95 modules. It provides greater platform integrity and strong encapsulation.
  • It enables the creation of a separate Java module as a modular JAR file for Java applications or Java API.

Java 9 Module System

  • In Java 9 modules, the functionally related packages are aggregated into reusable groups called modules.
  • The Java 9 module may contain resources (such as images and XML files). java.base module is an independent module that is necessary for every module.
  • The System Modules are standard, and their names start with java.
  • For example, JavaFX modules(names starting with JavaFX), JDK modules(names starting with jdk), Oracle modules(names starting with oracle).

Types of Java 9 Modules

1. Application modules

  • These modules are created by programmers.
  • The assembled path unofficial JAR contains the compiled module-info.a class file that contains the name and definition of the module.

2. Automatic Modules

  • When existing JAR files are added to the module, automatic modules are created.
  • The name of the JAR becomes the name of the module.

3. Unnamed Module

  • It’s a class or JAR loaded onto the classpath but not into the module path.
  • It’s a catch-all module to maintain backward compatibility with previously-written Java code.

Modular Descriptor

  • The module-info.java file has the module definition or metadata that describes the module.
  • On compilation of module-info.java, we get module-info.class that contains the module descriptor, which is stored in the module’s root folder.
  • Modular descriptor consists of one or more exports and requires a clause.
  • It is an independent module as it can export the packages to other modules and can use the packages of other modules.
  • The metadata includes three things, unique name, exports clause, and requires clause.
  • Unique name: Name given to module.

Code:

module eg.com.module1 
{

}
  • Exports Clause: The packages in the module can be exported as other modules can use them.

Code:

module eg.com.module1 
{
   exports eg.com.module1.service;
}
  • Requires Clause: It is used when you are in need of packages from other modules.

Code:

{
   requires eg.com.module1;
}

Modules of Java 9 Module System Application

  • To list out the modules of the JDK’s set from the bin folder of jdk. Run the following command in CLI java –list-modules
  • The keyword module is used to declare the module. The module body is enclosed in braces, as in the: module modulename { }
  • An exports module directive makes the module’s package accessible to code in all other modules.
  • An exports…to define a qualified report. The list of packages that can access the exported package can be precisely specified in a comma-separated list.

Code:

module com.educba.util 
{
exports com.educba.app;
exports com.educba.security to com.educba.client, com.educba.producer;
}
  • When an open module directive is used, all the packages in a given module become accessible at runtime and via reflection to all other modules. To allow runtime-only access to all packages in a module, you may open the entire module, as in:

Code;

open module <em>modulename</em>

{
// module directives
}
  • To allow runtime-only access to packages in a module. An open module directive of the form opens package- can be used. The code in other modules can access the public types of a package and its nested public and protected types at runtime only. Reflection can be used to access all the types and the types’ members in the specified package.
  • The code in the listed modules can access the public types of a package and its nested public and protected types at runtime only. Reflection to code in the specified modules can be used to access all of the types and types’ members in the specified package.

How to Create a New Java 9 Module?

  • Create a directory structure
src\com.educba\com\educba
  • In the command prompt
mkdir –r src\com.educba\com\educba
  • Create a new module in a text editor and save it as a java file module-info.java.
module com.educba
{ }
  • Set the environment variable JAVA_HOME to the path where Java is installed.

For example, JAVA_HOME C:\Program Files\Java\jdk-9\bin

Add %JAVA_HOME% to PATH in control Panel->system->advanced system setting->environment variables.

  • To see the compiled module descriptor in the CLI.

Code:

>javap mods/com.educba/module-info.class

Output:

Java 9 Modules

It shows requires java.base in the modules definition, though we had created an empty module. It is because java.base is the basic and independent module. All other modules depend on it.

Comparison Table of JDK 8 and JDK 9

The most obvious difference between JDK 8 and JDK 9.

Feature Java 8 Java 9
Top Level component Package Module
New features launched ●       Lambda Expressions

●       Stream API

●       Date API

●       Java Module System (Jigsaw Project)

●       Java REPL

●       Milling Project Coin

Performance Compromised performance due to big size of jdk Improved performance
Testing and maintaining applications Difficult with large size of JRE Easy with REPL and reduced size depending on modules included. JShell or REPL: Read and evaluate Print Loop for easy execution and testing of   Java Constructs like class, interface, enum, object, and statements easily.
Security It is compromised because of no encapsulation. Internal APIs can be accessed easily by users and potential hackers. Reflection could be used to learn about private members too. String security as Reflection does not provide access to private members. Only those exposed by export keywords are available through reflection.
Packaging format JAR JMOD can include native code and configuration files.

Conclusion

Java 9 is indeed the most sought restructuring of java code to make it compatible with the new coding techniques of distributed architecture. It has helped in reducing the size of executables with the choice of including the required modules, hence improving performance. The strong encapsulation has enabled higher security with fewer classes available to potential hackers. The modularity has provided transparency in dependency declaration and determination.

FAQs

Q1. How do I migrate my legacy application into JAVA 9?

Answer: You can compile your legacy application in Java 9. On compilation, you will encounter errors for those parts of code that are not in the modular structure of Java 9. Here you need to spend some time to include the required modules. You can test your new code using Jshell for the required output.

 Q2. How do I know which modules are provided by the system?

Answer: The java –list-modules can be used to list out the modules. You need to refer to the documentation to gain knowledge about their functions.

Q3. Is it necessary to install IDE to work in JAVA?

Answer: No. It depends on how comfortable you are with the Command line interface. IDEs do many jobs at the backend that are not known to users. CLI is the best option to identify the problem and resolve it.

 Q4. How to access a public package in my module?

Answer: You need to add the following in the module definition in your module to access the public package,  info.java, and required_package_name.

The above is the detailed content of Java 9 Modules. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JVM performance vs other languagesJVM performance vs other languagesMay 14, 2025 am 12:16 AM

JVM'sperformanceiscompetitivewithotherruntimes,offeringabalanceofspeed,safety,andproductivity.1)JVMusesJITcompilationfordynamicoptimizations.2)C offersnativeperformancebutlacksJVM'ssafetyfeatures.3)Pythonisslowerbuteasiertouse.4)JavaScript'sJITisles

Java Platform Independence: Examples of useJava Platform Independence: Examples of useMay 14, 2025 am 12:14 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunonanyplatformwithaJVM.1)Codeiscompiledintobytecode,notmachine-specificcode.2)BytecodeisinterpretedbytheJVM,enablingcross-platformexecution.3)Developersshouldtestacross

JVM Architecture: A Deep Dive into the Java Virtual MachineJVM Architecture: A Deep Dive into the Java Virtual MachineMay 14, 2025 am 12:12 AM

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVM: Is JVM related to the OS?JVM: Is JVM related to the OS?May 14, 2025 am 12:11 AM

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java: Write Once, Run Anywhere (WORA) - A Deep Dive into Platform IndependenceJava: Write Once, Run Anywhere (WORA) - A Deep Dive into Platform IndependenceMay 14, 2025 am 12:05 AM

Java implementation "write once, run everywhere" is compiled into bytecode and run on a Java virtual machine (JVM). 1) Write Java code and compile it into bytecode. 2) Bytecode runs on any platform with JVM installed. 3) Use Java native interface (JNI) to handle platform-specific functions. Despite challenges such as JVM consistency and the use of platform-specific libraries, WORA greatly improves development efficiency and deployment flexibility.

Java Platform Independence: Compatibility with different OSJava Platform Independence: Compatibility with different OSMay 13, 2025 am 12:11 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

What features make java still powerfulWhat features make java still powerfulMay 13, 2025 am 12:05 AM

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

Top Java Features: A Comprehensive Guide for DevelopersTop Java Features: A Comprehensive Guide for DevelopersMay 13, 2025 am 12:04 AM

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools