How to Create a module in java?

Earlier we supposed that our java module name is org.w3wiki 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.

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.w3wiki save the file as module-info.java as mentioned in the above image.

module  org.w3wiki { 
//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.w3wiki/org/w3wiki 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.

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.

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.w3wiki {  
requires java.base;
}


Java Modules

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.

Similar Reads

Difference Between Module and Package

The principal difference between the module and package is given below....

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

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

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

Contact Us