Open In App

Java Modules

Last Updated : 02 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
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;
}


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads