Java ModulesWhat are Java Modules? When to use? Why to use? How to create?

Aman Junaid
3 min readOct 10, 2022
Beautiful scenary
Photo by Gary Ellis on Unsplash

What?

What are Java Modules? — Java Module is a packaging mechanism where you can package one or more closely related packages and resources such as classes or images or xml files. Its a high-level code aggregation. Packages in a module are by default hidden unless they are exposed explicitly to a particular module or to all modules in general.

Module Categories —

  1. Java SE Modules : Core Java plate form, general-purpose APIs. Name starts with java, example — java.base, java.se, java.logging, etc
  2. JDK Modules : implementation specific modules. Name starts with jdk, example — jdk.httpserver, jdk.jconsole, jdk.jshell, etc.
  3. Other Modules : third party modules.

When?

When Java Modules were introduced? — Java modules were introduced in Java 9 through Java Platform Module System (JPMS) also called Project Jigsaw and along with this release, the JDK API’s are also packaged as modules.

Is it mandatory to use Java Modules? — No, we do not really need to implement Java Modules. Java 9 and later releases support old jar packaging by creating an unnamed module file and will continue. It is totally up to the developer if they want to use the Java Modules or not.

Why?

Why do we need Java Modules? — Currently when we deploy a java application, the package contains so many unnecessary classes and libraries which are there in java. Java modules helps in packaging with only a fixed number of classes and libraries required for running the application.

  1. Enables smaller application deployment footprint.
  2. Dependencies between modules can be verified on application startup.
  3. Deployment errors, such as missing modules are detected at startup.
  4. With module-path, the applications take less time to start. Classes are accessed by using module-path, not the class path. With the class path, you do not exactly know in which archive the class exists, so to load a class JVM has to scan all the archive to find where the class is, where as in modules, the JVM exactly know which archive the class exists.

Non-modular Java Application
Non-module java applications compile the project files and create a package like a jar file. The contents of the file are basically the compiles class files. Optionally you can add a descriptor MANIFEST.MF to describe what the archive contains, for ex-it contains info about the class which contains the main method, contains info about the class path of other jars, etc.

Concerns with Non-Modular approach —

  1. Classes are grouped into packages but the packages doesn’t impose any physical restrictions on the classes.
  2. Classes are packaged into jar file, and accessed via class path.
  3. Its impossible to apply restriction in which classes some code can be used.
  4. The visibility of the classes is controlled by access modifiers which provide compile time restrictions but the encapsulation can be by-passed using Reflection. Here is a example —

How to create a Module?

Java Module outline/structure- Java Module contains a module-info.class which has all the info related to the module.

Here is the github link for this project —

More updates on the way😊..

--

--