Automatic Modules in Java

Last Updated : 19 Oct, 2026

An automatic module is a JAR file without a module descriptor that is placed on the module path. It allows existing non-modular libraries to work with the Java Module System without adding module-info.java.

  • Its module name is derived from the Automatic-Module-Name manifest entry or the JAR filename.
  • It exports all its packages and reads all other modules, making migration to JPMS easier.

Features of Automatic Modules

An automatic module provides the following features:

  • It does not require a module-info.java file.
  • It receives a module name from the manifest or JAR filename.
  • All packages in the automatic module are exported.
  • Its packages are also open for deep reflection.
  • It reads all other modules, including the unnamed module.
  • Other modules may declare a dependency on it using the requires directive.

Naming of Automatic Modules

Java determines the name of an automatic module in two steps.

1. Automatic-Module-Name

If the JAR manifest contains the Automatic-Module-Name attribute, its value is used as the module name.

Automatic-Module-Name: easy.math
The resulting module name is: easy.math

UsingĀ Automatic-Module-NameĀ provides a more stable module name than relying on the JAR filename.

2. JAR File Name

If Automatic-Module-Name is not present, Java derives the module name from the JAR filename.

The main rules are:

  • Remove the .jar extension.
  • If the filename contains a version pattern such as -1.2.3, the version portion is ignored when deriving the name.
  • Replace non-alphanumeric characters with dots.
  • Replace consecutive dots with a single dot.
  • Remove leading and trailing dots.

easy-math-1.0.jar

is derived as: easy.math

Uses of Automatic Modules

Automatic modules are useful when an application is being migrated to the Java module system.

  • They allow existing JAR libraries to be placed on the module path.
  • They allow a modular application to declare dependencies on libraries that do not yet contain a module descriptor.
  • They export all packages, making the public types of those packages accessible to modules that read the automatic module.
  • They read all other modules, which helps existing libraries work within a modular application.
  • They support gradual modularization instead of requiring all dependencies to become explicit modules at the same time.

Example

An automatic module is created when a non-modular JAR is placed on the module path. In this example, easy-math.jar does not contain a module-info.class, so Java treats it as an automatic module.

Project Structure

Create the following project structure:

Screenshot

Step 1: Create the Library Class

Create Calculate.java at:

src\simple.add\simple\add\Calculate.java

Java
package simple.add;

public class Calculate {

    public static int sum(int a, int b) {
        return a + b;
    }
}

This class provides the sum() method, which adds two integers.

Step 2: Create the JAR

The library does not contain a module-info.java, so it is a non-modular JAR.

Compile the library:

javac -d lib\classes src\simple.add\simple\add\Calculate.java

Step 3: Create the Application Class

Create GFG.java at:

src\add.app\com\example\GFG.java

Java
package com.example;

import simple.add.Calculate;

public class GFG {

    public static void main(String[] args) {

        int sum = Calculate.sum(10, 7);

        System.out.println("Sum is " + sum);
    }
}

The application imports Calculate from the non-modular JAR and calls its sum() method.

Step 4: Create the Module Descriptor

Create module-info.java at:

src\add.app\module-info.java

module add.app {
requires easy.math;
}

Here, requires easy.math; declares a dependency on the automatic module. The automatic module name is easy.math.

Step 5: Compile the Application

Open Terminal in Module Location and Run.

javac -d out --module-path lib --module-source-path src -m add.app

The command compiles the add.app module and uses easy-math.jar from the module path.

Step 6: Run the Application

Run:

java --module-path "lib;out" -m add.app/com.example.GFG

Output

Screenshot

Explanation: The application module add.app declares requires easy.math;. The JAR easy-math.jar does not contain an explicit module descriptor, so Java treats it as an automatic module when it is placed on the module path. The application then accesses the Calculate class from this automatic module and prints the result returned by sum().

Comment