Java / JPMS
The Java Platform Module System (JPMS), introduced in Java 9, is a fundamental change to the structure of the Java platform. It aims to improve the security, maintainability, and scalability of Java applications. JPMS achieves this by introducing the concept of modules, which are self-contained units of code and resources with explicit dependencies.
Modularity: JPMS divides code into modules, each with a module-info.java file declaring its dependencies and exported packages.
Strong Encapsulation: Modules can control which packages are accessible to other modules, enhancing security and reducing unintended dependencies.
Reliable Configuration: JPMS enforces explicit dependencies, ensuring that all required modules are present at compile and runtime.
Scalability: By breaking down large applications into smaller, manageable modules, JPMS facilitates development and maintenance.
Reduced Runtime Size: Applications can be deployed with only the necessary modules, reducing the footprint and improving performance.
System Modules: Modules that comprise the Java SE platform and the JDK.
Application Modules: Custom modules created by developers for their applications.
Automatic Modules: JAR files on the module path that are treated as modules, providing compatibility with older libraries.
Unnamed Module: A catch-all module for classes loaded from the classpath, ensuring backward compatibility.
JPMS introduces a new level of abstraction above packages. Modules are defined using the module keyword in a module-info.java file. This file specifies:
Module Name: A unique identifier for the module.
Dependencies (requires): Other modules that this module depends on.
Exported Packages (exports): Packages that are accessible to other modules.
Provided Services (provides): Services that this module offers.
Consumed Services (uses): Services that this module uses.
Improved Security: Strong encapsulation prevents unauthorized access to internal APIs.
Better Maintainability: Modular code is easier to understand, test, and modify.
Increased Reliability: Explicit dependencies ensure that all required components are present.
Enhanced Scalability: Large applications can be broken down into smaller, manageable units.
Reduced Complexity: JPMS simplifies dependency management and reduces the risk of conflicts.
Define Modules: Create module-info.java files for each module.
Compile Modules: Use the javac compiler with module path options.
Package Modules: Package modules into JAR files.
Run Applications: Use the java command with module path options.
// module-info.java module com.example.mymodule { requires java.net.http; exports com.example.mymodule.api; } // MyClass.java package com.example.mymodule.api; public class MyClass { public static String getMessage() { return "Hello from my module!"; } } // Main.java package com.example.myapp; import com.example.mymodule.api.MyClass; public class Main { public static void main(String[] args) { System.out.println(MyClass.getMessage()); } }