Requires Directive in Java Modules

Last Updated : 29 Sep, 2026

The requires directive in Java modules is used to declare a dependency on another module. It tells the Java Module System that the current module reads another module and depends on it for its functionality.

  • It establishes a dependency between two named modules.
  • A normal requires dependency is required during compilation and runtime.
  • The transitive and static modifiers provide additional control over dependency readability and runtime requirements.

Syntax

requires moduleName;

Example

module com.example.app {
requires com.example.library;
}

  • requires: Declares a dependency on another module.
  • moduleName: Specifies the name of the module that the current module depends on.

Working Of Requires Directive

The requires directive establishes a relationship between the current module and another module. The requires directive tells the module system which module the current module depends on.

  1. Declare Dependency: The current module specifies the required module using requires.
  2. Read Required Module: The current module gets readability access to the required module.
  3. Resolve Dependency: The module system resolves the required module during compilation and runtime.
  4. Access Exported Packages: The current module accesses packages exported by the required module.
  5. Run Application: The required module must be available when the dependency is required at runtime.

Types of Requires Directive

The requires directive supports different forms for controlling module dependencies.

1. Normal Requires

A normal requires directive creates a regular dependency between two modules.

module com.example.app {
requires com.example.library;
}

Here, com.example.app reads com.example.library. The dependency is part of the module's declaration and is considered during module resolution.

2. Requires Transitive

The transitive modifier makes the dependency readable to modules that depend on the current module.

module com.example.library {
requires transitive com.example.utility;
}

If another module requires com.example.library, it also gets readability of com.example.utility.

3. Requires Static

The static modifier declares a dependency that is required during compilation but optional at runtime.

module com.example.app {
requires static com.example.optional;
}

This form is useful for optional dependencies that are needed to compile the module but are not required in every runtime environment.

4. Requires Static Transitive

A requires static dependency is required during compilation but optional during runtime. The required module must be available when compiling the module, but it does not have to be present when the application starts.

module com.example.library {
requires static transitive com.example.utility;
}

Using Requires Directive

Suppose an application module needs to use a class from a separate library module. We use the requires directive in the application module to declare this dependency. The library module also exports the package containing the class used by the application.

Project Structure

Screenshot-
Project structure containing the application and library modules.

Library Module

First, create the com.example.library module.

module com.example.library {
exports com.example.library;
}

The exports directive makes the com.example.library package available to other modules that read this module.

Message.java

Java
package com.example.library;

public class Message {
    public static void show() {
        System.out.println("Message from library module");
    }
}

The Message class belongs to the com.example.library package and provides the show() method.

Application Module

Now, create the com.example.app module and declare its dependency on the library module.

module-info.java

module com.example.app {
requires com.example.library;
}

The requires directive tells the Java Module System that com.example.app depends on com.example.library. This establishes readability between the two modules.

GFG.java

Java
package com.example.app;

import com.example.library.Message;

public class GFG {
    public static void main(String[] args) {
        Message.show();
    }
}

The GFG class imports Message from the library module and calls its show() method.

Compile The Modules

Open PowerShell in the project directory.
Compile both modules using:

javac -d out --module-source-path src -m com.example.library,com.example.app

Here, --module-source-path src specifies the source location containing the modules, while -m specifies the modules to compile. When multiple modules are compiled, javac uses the module source path to locate their source files.

Run The Application

Run the application module using:

java --module-path out -m com.example.app/com.example.app.GFG

Here, --module-path out specifies the compiled modules, and -m specifies the module and main class to run.

Output

Screenshot-

Explanation: com.example.app uses the Message class from com.example.library. The requires com.example.library directive establishes the module dependency, while exports com.example.library makes the package accessible to the application module.

Rules of Requires Directive

The requires directive follows several rules:

  1. A module may declare a dependency on another named module using requires.
  2. The same module name must not appear in more than one requires directive in the same module declaration.
  3. requires transitive passes readability to modules that depend on the current module.
  4. requires static makes the dependency mandatory during compilation but optional during runtime.
  5. java.base is implicitly required by every module except java.base itself.
  6. java.base must not be declared with requires static.
  7. A module dependency participates in module resolution during compilation and runtime.
Comment