Java Module System provides an additional layer of encapsulation to our programs as we can specify which package can be utilized by the modules, and which modules could have entry to to them. Before entering into the exclusive types of modules in Java, we will first learn the differences among applications and modules. We recognize that there are numerous applications in Java including IO bundle, AWT package, swing bundle, and so on.
Difference Between Module and Package
The principal difference between the module and package is given below.
1. Module: In easy words, we can say that the module is a set of related applications or It is a collection of related applications such that it affords an API handy to different modules that are internal and encapsulated.
Suppose permits take an example from the built-in module in Java let's take java.util for example. If we expect java.util as a module we recognize that there are a variety of instructions and sub-packages inside the java.util. Now we've assumed that java.util is a package deal the modules could be like java.util.Collections and java.util.stream.
2. Package: A package is a set of classes, interfaces, and sub-packages that are similar. There are mainly two types of packages in Java, they are:
- User Defined packages: The packages that contain the classes or interfaces which are built based on the user and it is nothing but they are just defined by the user.
- Built-In Packages: The packages that come pre-installed when we configure the Java in our system are called the built-in packages. For example, as we specified earlier such as java.net, java.awt, javax.swing, java.sql etc
Java 9 Module System
Java 9 has one of the major changes in its features which is the Java module System. The main aim of the system is to collect Java packages and code to be collected into a single unit called a Module. The reason to add this feature in Java is that when we want to create modular applications using Java the earlier versions before 9 have no system like this that's why the size of the application has increased. Even the JDK file in the earlier version of Java has a large size only the rt.jar file size was around 64 MB.
To avoid this situation Java 9 has split the JDK into a set of modules. so that we can use the required module to develop our application. Apart from this, the Java 9 version has also provided a feature so that the user could create their own module and develop their own applications.
The modules in Java have the below options mentioned:
- This version of Java includes various options for Java tools such as javac and java etc. In which we can specify the module path and help us to locate the location of the module in the Java 9.
- The JAR [ java Archive] file was introduced in this version of java. The JAR contains module-info.class file in the folder. [ JAR is a file format which is a zip file which is used for aggregation of many files into one ]
- As we specified earlier the earlier versions of java has no modular system and the size of rt.jar file was large so the JDK and JRE was split into modules so that we can use the modules we want to develop our application.
- Another thing is that the java 9 has introduced a new URL method for naming the class and modules.
- JMOD file format is also introduced which is used to handle other configuration files in java.
Java 9 Module
The collection of packages and code into a single unit is called module . The module can be described with the help of module descriptor which is named by module-info.java and the module declarator describes the below three that were shown in the image.

Name of the module
The name of the module is named on the basis on the reverse domain pattern. It is mainly used to overcome the naming conflict in java. suppose we have the domain names as geeksforgeeks.org the module name can be org.geeksforgeeks .
The next point is that it should contain what does the module contains and what does the module export in the java
How to Create a module in java?
Earlier we supposed that our java module name is org.geeksforgeeks which has followed the reverse domain pattern to overcome the naming conflicts.
Creating the java modules involves three steps to be created in java. The three steps to be followed are:
- We want to create a directory structure.
- We want to create a module declarator as we mentioned earlier which is used to describe the module.
- we have to create a source code in that module
Step 1 : (Create a Directory Structure)
We have to create a directory structure mentioned below . In order to create a module we have to follow the reverse domain pattern in a similiar way we create packages in java.
-768.png)
Step 2 - Create a module declarator
Create a file name module-info.java as module declarator and in the interior of the file create a module using the module identifier . After the module identifier use the module name same as directory name and if the module-info.java has no dependency leave it empty and save it in the as mentioned below:
In the src/org.geeksforgeeks save the file as module-info.java as mentioned in the above image.
module org.geeksforgeeks {
//empty body
}
Write the above mentioned code in the module-info.java file.
Step 3 - Create a source code file
Now we can create the source code file with the name Main.java and save it and save the file in the src/org.geeksforgeeks/org/geeksforgeeks as mentioned and shown in the above image.
The source code is mentioned in the below block:
Java //Java program to create a source code file class Main { public static void main(String args[]) { System.out.println("Hello welcome geek and know about java 9 module system"); } }
OutputHello welcome geek and know about java 9 module system
Compilation of java Module
To compile the java module, run the below command so that we will get the directory structure as mentioned below.

It will show and create the following directory structure after compiling the above command and after executing the module-info.class and Main.class are generated and which will be under the module_name that we specified in the above command after -d which keeps in the destination and it is shown in the below structure.
.png)
Run the source code in the module
Use the below command in the java to run the module and to get the Main.java source code that is present in the module.

Note: A module can contain one or many classes.
Compiled module descriptor [ module-info file]
Now if we want to see the module descriptor file, we can run the following command and see the output.

Output
module org.geeksforgeeks {
requires java.base;
}
Similar Reads
Java Methods
Java Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving both efficiency and organization. All methods in Java must belong to a class. Methods are similar to functions and expose the behavior of objects. Example: Java program to demonstrate how to cre
8 min read
Java Keywords
In Java, keywords are the reserved words that have some predefined meanings and are used by the Java compiler for some internal process or represent some predefined actions. These words cannot be used as identifiers such as variable names, method names, class names, or object names. Now, let us go t
5 min read
Julia vs. Java
What is Java? Java is one of the most popular and widely used programming language and platform. A platform is an environment that helps to develop and run programs written in any programming language. Julia is a high level, high performance, dynamic programming language. In this article, the differ
3 min read
Java Operators
Java operators are special symbols that perform operations on variables or values. These operators are essential in programming as they allow you to manipulate data efficiently. They can be classified into different categories based on their functionality. In this article, we will explore different
15 min read
Java Variables
In Java, variables are containers that store data in memory. Understanding variables plays a very important role as it defines how data is stored, accessed, and manipulated. Key Components of Variables in Java: A variable in Java has three components, which are listed below: Data Type: Defines the k
9 min read
Java JFrame
Thе Java JFramе is an essential componеnt of Java Swing, which is a part of thе Java SWT(Standard Widgеt Toolkit). JFrame in Java is a class that allows you to crеatе and manage a top-lеvеl window in a Java application. It sеrvеs as thе main window for GUI-basеd Java applications and providеs a plat
4 min read
Java JComponent
In Java Swing, JComponent is an abstract class that programmers can use to modify to build unique components that are suited to the particular requirements of their applications. JComponent and other Swing components are lighter than their AWT equivalents. The Java runtime renders lightweight compon
6 min read
Service Client Module in Java
A Client Module in Java is a set of classes and methods that are used to connect to, interact with, and consume services from a server. It is the front-end component of a client/server architecture. It is typically responsible for initiating communication with the server, sending and receiving data,
6 min read
Perl vs Java
Perl was developed in 1987 by Larry Wall. Perl Supports object-oriented as well as procedural programming. It is a lot like C and C++. Perl was originally developed for text processing. Java is one of the widely used programming language. Not only Java is a programming language but also a computing
3 min read
Java Tutorial
Java is a high-level, object-oriented programming language used to build applications across platformsâfrom web and mobile apps to enterprise software. It is known for its Write Once, Run Anywhere capability, meaning code written in Java can run on any device that supports the Java Virtual Machine (
11 min read