Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java Keywords
Next article icon

Java Modules

Last Updated : 02 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Java Module System

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.

Directory Structure

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");   } } 

Output
Hello 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.

Compilation of java module

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.

Directory Structure

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.

Running_sourcecode

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.

Module -descriptor compilation

Output

module org.geeksforgeeks {  
requires java.base;
}

Next Article
Java Keywords
author
boora_harsha_vardhan
Improve
Article Tags :
  • Java
  • Geeks Premier League
  • Geeks Premier League 2023
Practice Tags :
  • Java

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences