Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The requires directive is a module declaration statement within a module-info.java file used to specify a dependency on another module. Introduced in the Java Platform Module System (JPMS), it establishes a formal “reads” relationship. This dictates that the declaring module depends on the target module to successfully compile and execute.

Syntax

module <module.name> {
    requires [modifiers] <target.module.name>;
}

Mechanics and Resolution

When the Java compiler or the JVM encounters a requires directive, it searches the module path for the specified target module. If the target module is found, the module system grants the requiring module readability of the target module. This allows the requiring module to access public types in packages that the target module exports unconditionally, or exports specifically to the requiring module via a qualified exports ... to directive. If a required module is missing from the module path, the compiler emits an error, and the JVM throws a java.lang.module.FindException at startup. Other graph resolution failures, such as cyclic dependencies or split packages, result in a java.lang.module.ResolutionException. By default, the java.base module is implicitly required by all other Java modules. Explicitly declaring requires java.base; is permitted but redundant. Furthermore, the JPMS strictly prohibits cyclic dependencies; if module A requires module B, module B cannot require module A (neither directly nor transitively).

Modifiers

The requires directive accepts specific modifiers that alter the strictness and propagation of the dependency.

Standard requires

A standard requires statement without modifiers creates a strict, encapsulated dependency required at both compile-time and runtime. Modules that depend on the declaring module do not inherit readability of this required module.
module com.example.core {
    requires java.sql;
}

requires transitive

The transitive modifier establishes “implied readability.” If Module A declares requires transitive on Module B, any Module C that requires Module A will automatically be granted readability of Module B. This is mechanically necessary when types from the required module are exposed in the public API signatures of the declaring module.
module com.example.api {
    requires transitive java.xml;
}

requires static

The static modifier creates a compile-time dependency that is strictly optional at runtime. The module system mandates the presence of the target module during compilation but does not require it for runtime resolution. If the target module is absent at runtime, the JVM starts normally without throwing a FindException. However, if the target module is present and resolved at runtime (for example, because it is required by another module in the resolved graph or added manually via --add-modules), the reads relationship is actively established, and the requiring module can use its types normally without reflection.
module com.example.utils {
    requires static java.compiler;
}

requires transitive static

Modifiers can be combined to create a dependency that is optional at runtime but transitively propagated to dependent modules at compile-time. The order of the modifiers does not matter (requires static transitive is also valid).
module com.example.framework {
    requires transitive static java.desktop;
}
Master Java with Deep Grasping Methodology!Learn More