Java tutorials > Modern Java Features > Java 8 and Later > What are modules in Java 9 (Project Jigsaw)?
What are modules in Java 9 (Project Jigsaw)?
Java 9 introduced modules as part of Project Jigsaw. Modules are a way to encapsulate and organize Java code, enhancing security, maintainability, and performance. Prior to Java 9, Java applications suffered from the problem of tightly coupled components and a lack of strong encapsulation. Every public class was visible to every other class on the classpath. Modules address this by providing a strong mechanism for defining dependencies and controlling access to internal APIs.
Core Concepts Behind Modules
Modules introduce several key concepts:
module-info.java
): Each module has a module-info.java
file that declares its name, dependencies (requires
), and which packages it exposes to other modules (exports
).requires
keyword. This allows the JVM to ensure that all required modules are present at runtime.
Module Declaration Example (module-info.java)
This The `opens` keyword allows runtime reflection on the `com.example.mymodule.config` package. This is often used by frameworks like Spring or Hibernate.module-info.java
file defines a module named com.example.mymodule
. It declares a dependency on the java.sql
module (part of the JDK). It also exports the com.example.mymodule.api
package, making its public classes accessible to other modules. The line `exports com.example.mymodule.utils to com.example.anothermodule;` demonstrates qualified exports, where the package is only visible to the specified module.
module com.example.mymodule {
requires java.sql; // Depends on the java.sql module
exports com.example.mymodule.api; // Exposes the API package
exports com.example.mymodule.utils to com.example.anothermodule; // exports to a specific module
opens com.example.mymodule.config; // opens package for reflection
}
Creating a Simple Module
This code demonstrates a simple module. The MyService
class is part of the com.example.mymodule.api
package. The module-info.java
file exports this package, making the MyService
class accessible to other modules.
// src/com.example.mymodule/com/example/mymodule/api/MyService.java
package com.example.mymodule.api;
public class MyService {
public String getMessage() {
return "Hello from MyModule!";
}
}
// src/com.example.mymodule/module-info.java
module com.example.mymodule {
exports com.example.mymodule.api;
}
Using a Module
This code shows how to use the com.example.mymodule
module from another module, com.example.anothermodule
. The module-info.java
file for com.example.anothermodule
declares that it requires com.example.mymodule
. This allows the Main
class to import and use the MyService
class.
// src/com.example.anothermodule/com/example/anothermodule/Main.java
package com.example.anothermodule;
import com.example.mymodule.api.MyService;
public class Main {
public static void main(String[] args) {
MyService service = new MyService();
System.out.println(service.getMessage());
}
}
// src/com.example.anothermodule/module-info.java
module com.example.anothermodule {
requires com.example.mymodule;
}
Real-Life Use Case
Consider a large enterprise application with many internal libraries. Using modules allows you to:
Best Practices
Interview Tip
When discussing modules in interviews, be prepared to explain:
When to Use Modules
Modules are particularly beneficial in the following scenarios:
Memory Footprint
Modules can reduce the memory footprint of Java applications by:
Alternatives
Before Java 9, developers often used techniques like OSGi or custom class loaders to achieve some level of modularity. However, these approaches were often complex and had limitations.
Pros of Modules
Cons of Modules
FAQ
-
What is the
requires transitive
keyword?
The
requires transitive
keyword means that if module A requires module B transitively, any module that requires A will also implicitly require B. This is useful when module B's API is an integral part of module A's API. -
What are automatic modules?
Automatic modules are created when a JAR file is placed on the module path without a
module-info.java
file. The module name is derived from the JAR file name. Automatic modules can access any other module but do not provide strong encapsulation. -
How do I run a modular application?
You can run a modular application using the
java
command with the--module-path
and--module
options. For example:java --module-path mods -m com.example.mymodule/com.example.mymodule.Main
.