Java Tutorial
- Java - Home
- Java - Overview
- Java - History
- Java - Features
- Java vs C++
- Java Virtual Machine (JVM)
- Java - JDK vs JRE vs JVM
- Java - Hello World Program
- Java - Environment Setup
- Java - Basic Syntax
- Java - Variable Types
- Java - Data Types
- Java - Type Casting
- Java - Unicode System
- Java - Basic Operators
- Java - Comments
Java Control Statements
- Java - Loop Control
- Java - Decision Making
- Java - If-else
- Java - Switch
- Java - For Loops
- Java - For-Each Loops
- Java - While Loops
- Java - do-while Loops
- Java - Break
- Java - Continue
Object Oriented Programming
- Java - OOPs Concepts
- Java - Object & Classes
- Java - Class Attributes
- Java - Class Methods
- Java - Methods
- Java - Variables Scope
- Java - Constructors
- Java - Access Modifiers
- Java - Inheritance
- Java - Aggregation
- Java - Polymorphism
- Java - Overriding
- Java - Method Overloading
- Java - Dynamic Binding
- Java - Static Binding
- Java - Instance Initializer Block
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java - Inner Classes
- Java - Static Class
- Java - Anonymous Class
- Java - Singleton Class
- Java - Wrapper Classes
- Java - Enums
- Java - Enum Constructor
- Java - Enum Strings
Java Built-in Classes
Java File Handling
- Java - Files
- Java - Create a File
- Java - Write to File
- Java - Read Files
- Java - Delete Files
- Java - Directories
- Java - I/O Streams
Java Error & Exceptions
- Java - Exceptions
- Java - try-catch Block
- Java - try-with-resources
- Java - Multi-catch Block
- Java - Nested try Block
- Java - Finally Block
- Java - throw Exception
- Java - Exception Propagation
- Java - Built-in Exceptions
- Java - Custom Exception
Java Multithreading
- Java - Multithreading
- Java - Thread Life Cycle
- Java - Creating a Thread
- Java - Starting a Thread
- Java - Joining Threads
- Java - Naming Thread
- Java - Thread Scheduler
- Java - Thread Pools
- Java - Main Thread
- Java - Thread Priority
- Java - Daemon Threads
- Java - Thread Group
- Java - Shutdown Hook
Java Synchronization
- Java - Synchronization
- Java - Block Synchronization
- Java - Static Synchronization
- Java - Inter-thread Communication
- Java - Thread Deadlock
- Java - Interrupting a Thread
- Java - Thread Control
- Java - Reentrant Monitor
Java Networking
- Java - Networking
- Java - Socket Programming
- Java - URL Processing
- Java - URL Class
- Java - URLConnection Class
- Java - HttpURLConnection Class
- Java - Socket Class
- Java - Generics
Java Collections
Java Interfaces
- Java - List Interface
- Java - Queue Interface
- Java - Map Interface
- Java - SortedMap Interface
- Java - Set Interface
- Java - SortedSet Interface
Java Data Structures
Java Collections Algorithms
Advanced Java
- Java - Command-Line Arguments
- Java - Lambda Expressions
- Java - Sending Email
- Java - Applet Basics
- Java - Javadoc Comments
- Java - Autoboxing and Unboxing
- Java - File Mismatch Method
- Java - REPL (JShell)
- Java - Multi-Release Jar Files
- Java - Private Interface Methods
- Java - Inner Class Diamond Operator
- Java - Multiresolution Image API
- Java - Collection Factory Methods
- Java - Module System
- Java - Nashorn JavaScript
- Java - Optional Class
- Java - Method References
- Java - Functional Interfaces
- Java - Default Methods
- Java - Base64 Encode Decode
- Java - Switch Expressions
- Java - Teeing Collectors
- Java - Microbenchmark
- Java - Text Blocks
- Java - Dynamic CDS archive
- Java - Z Garbage Collector (ZGC)
- Java - Null Pointer Exception
- Java - Packaging Tools
- Java - Sealed Classes
- Java - Record Classes
- Java - Hidden Classes
- Java - Pattern Matching
- Java - Compact Number Formatting
- Java - Garbage Collection
- Java - JIT Compiler
Java Miscellaneous
- Java - Recursion
- Java - Regular Expressions
- Java - Serialization
- Java - Strings
- Java - Process API Improvements
- Java - Stream API Improvements
- Java - Enhanced @Deprecated Annotation
- Java - CompletableFuture API Improvements
- Java - Streams
- Java - Datetime Api
- Java 8 - New Features
- Java 9 - New Features
- Java 10 - New Features
- Java 11 - New Features
- Java 12 - New Features
- Java 13 - New Features
- Java 14 - New Features
- Java 15 - New Features
- Java 16 - New Features
Java APIs & Frameworks
Java Class References
- Java - Scanner Class
- Java - Arrays Class
- Java - Strings
- Java - Date & Time
- Java - ArrayList
- Java - Vector Class
- Java - Stack Class
- Java - PriorityQueue
- Java - LinkedList
- Java - ArrayDeque
- Java - HashMap
- Java - LinkedHashMap
- Java - WeakHashMap
- Java - EnumMap
- Java - TreeMap
- Java - The IdentityHashMap Class
- Java - HashSet
- Java - EnumSet
- Java - LinkedHashSet
- Java - TreeSet
- Java - BitSet Class
- Java - Dictionary
- Java - Hashtable
- Java - Properties
- Java - Collection Interface
- Java - Array Methods
Java Useful Resources
Java - Module System
In Java 9, Module system was introduced to enhance Java code modularity. Module is an abstraction over package. This module system is also known as JPMS, Java Platform Module System. It is mostly referred as Modules.
What is a Module?
A module is a self-describing collection of code and data and has a name to identify it. It a new kind of programming component which can contains packages, configurations, resources specific to a particular functionality. A module is able to restrict access to packages it contains. By default, code in a package within a module is not visible to outside world, not even via reflection. Java platform is itself modularised from java 9 onwards. If we use the list-modules command to list the java modules, it will print the various modules java supports as following:
C:\Users\Mahesh>java --list-modules java.base@20.0.2 java.compiler@20.0.2 java.datatransfer@20.0.2 java.desktop@20.0.2 java.instrument@20.0.2 java.logging@20.0.2 java.management@20.0.2 java.management.rmi@20.0.2 java.naming@20.0.2 java.net.http@20.0.2 java.prefs@20.0.2 java.rmi@20.0.2 java.scripting@20.0.2 java.se@20.0.2 java.security.jgss@20.0.2 java.security.sasl@20.0.2 java.smartcardio@20.0.2 java.sql@20.0.2 java.sql.rowset@20.0.2 java.transaction.xa@20.0.2 java.xml@20.0.2 java.xml.crypto@20.0.2 jdk.accessibility@20.0.2 jdk.attach@20.0.2 jdk.charsets@20.0.2 jdk.compiler@20.0.2 jdk.crypto.cryptoki@20.0.2 jdk.crypto.ec@20.0.2 jdk.crypto.mscapi@20.0.2 jdk.dynalink@20.0.2 jdk.editpad@20.0.2 jdk.hotspot.agent@20.0.2 jdk.httpserver@20.0.2 jdk.incubator.concurrent@20.0.2 jdk.incubator.vector@20.0.2 jdk.internal.ed@20.0.2 jdk.internal.jvmstat@20.0.2 jdk.internal.le@20.0.2 jdk.internal.opt@20.0.2 jdk.internal.vm.ci@20.0.2 jdk.internal.vm.compiler@20.0.2 jdk.internal.vm.compiler.management@20.0.2 jdk.jartool@20.0.2 jdk.javadoc@20.0.2 jdk.jcmd@20.0.2 jdk.jconsole@20.0.2 jdk.jdeps@20.0.2 jdk.jdi@20.0.2 jdk.jdwp.agent@20.0.2 jdk.jfr@20.0.2 jdk.jlink@20.0.2 jdk.jpackage@20.0.2 jdk.jshell@20.0.2 jdk.jsobject@20.0.2 jdk.jstatd@20.0.2 jdk.localedata@20.0.2 jdk.management@20.0.2 jdk.management.agent@20.0.2 jdk.management.jfr@20.0.2 jdk.naming.dns@20.0.2 jdk.naming.rmi@20.0.2 jdk.net@20.0.2 jdk.nio.mapmode@20.0.2 jdk.random@20.0.2 jdk.sctp@20.0.2 jdk.security.auth@20.0.2 jdk.security.jgss@20.0.2 jdk.unsupported@20.0.2 jdk.unsupported.desktop@20.0.2 jdk.xml.dom@20.0.2 jdk.zipfs@20.0.2 C:\Users\Mahesh>
Here we can see, that jdk specific packages are in jdk module and library specific packages are in java module.
Features of Java Module System
With the Modules component, following enhancements has been added in Java 9 −
A new optional phase, link time is introduced. This phase is in-between compile time and run time. During this phase, a set of modules can be assembled and optimized, making a custom runtime image using jlink tool.
javac, jlink, and java have additional options to specify module paths, which further locate definitions of modules.
JAR format updated as modular JAR, which contains module-info.class file in its root directory.
JMOD format introduced, a packaging format (similar to JAR) which can include native code and configuration files.
Declaring Module
In order to declare a module, we need to create a module-info.java in root folder of the application. This file contains all the meta data or module descriptions.
Example
module-info.java
module com.tutorialspoint.greetings { }
Adding Dependent Modules
We can declare dependencies of other modules in the module. For example, if we want to use com.tutorialspoint.util module then we can add the declaration as follows:
Example
module com.tutorialspoint.greetings { requires com.tutorialspoint.util; }
Adding Optional Modules
We can declare optional dependencies of other modules using static keyword in the module. For example, if we want to use com.tutorialspoint.logging module then we can add the declaration as follows:
Example
module com.tutorialspoint.greetings { requires com.tutorialspoint.util; requires static com.tutorialspoint.logging; }
Adding Transitive Modules
We can declare transitive dependencies of other modules using transitive keyword in the module. For example, if we want to use com.tutorialspoint.base module as dependency of com.tutorialspoint.util module then we can add the declaration as follows:
Example
module com.tutorialspoint.greetings { requires com.tutorialspoint.util; requires static com.tutorialspoint.logging; requires transitive com.tutorialspoint.base; }
This means if a module wants to use com.tutorialspoint.greetings then that module is required to add com.tutorialspoint.base module as well in module declaration.
Export Public Classes
By default, no public class of a package of a module is exposed to outside world. In order to use the public class, we've to export it as shown below:
Example
module com.tutorialspoint.greetings { requires com.tutorialspoint.util; requires static com.tutorialspoint.logging; requires transitive com.tutorialspoint.base; exports com.tutorialspoint.greetings.HelloWorld; }
Allow Reflection
By default, no private member of a package of a module is accessible via reflection. In order to allow reflection to inspect the class or module, we've to use opens command as shown below:
Example
module com.tutorialspoint.greetings { requires com.tutorialspoint.util; requires static com.tutorialspoint.logging; requires transitive com.tutorialspoint.base; exports com.tutorialspoint.greetings.HelloWorld; opens com.tutorialspoint.greetings.HelloWorld; }
If we need to allow complete module to be open for reflection, we can use open command as shown below:
open module com.tutorialspoint.greetings { requires com.tutorialspoint.util; requires static com.tutorialspoint.logging; requires transitive com.tutorialspoint.base; exports com.tutorialspoint.greetings.HelloWorld; }
Creating and Using Java Module
Following the steps to create a module say com.tutorialspoint.greetings.
Step 1
Create a folder C:\>JAVA\src. Now create a folder com.tutorialspoint.greetings which is same as the name of module we're creating.
Step 2
Create module-info.java in C:\>JAVA\src\com.tutorialspoint.greetings folder with following code.
module-info.java
module com.tutorialspoint.greetings { }
module-info.java is the file which is used to create module. In this step we've created a module named com.tutorialspoint.greetings. By convention this file should reside in the folder whose name is same as module name.
Step 3
Add the source code in the module. Create Java9Tester.java in C:\>JAVA\src\com.tutorialspoint.greetings\com\ tutorialspoint\greetings folder with following code.
Java9Tester.java
package com.tutorialspoint.greetings; public class Java9Tester { public static void main(String[] args) { System.out.println("Hello World!"); } }
By convention, the source code of a module to lie in same directory which is the name of the module.
Step 4
Create a folder C:\>JAVA\mods. Now create a folder com.tutorialspoint.greetings which is same as the name of module we've created. Now compile the module to mods directory.
C:/ > JAVA > javac -d mods/com.tutorialspoint.greetings src/com.tutorialspoint.greetings/module-info.java src/com.tutorialspoint.greetings/com/tutorialspoint/greetings/Java9Tester.java
Step 5
Let's run the module to see the result. Run the following command.
C:/>JAVA>java --module-path mods -m com.tutorialspoint.greetings/com.tutorialspoint.greetings.Java9Tester
Here module-path provides the module location as mods and -m signifies the main module.
It will print the following output on console.
Hello World!