Multi-Release JARs and Modules
You'll learn how to combine modular JARs with multi-release JAR features to ship a single artifact that adapts to different Java versions while maintaining module system compatibility.
What Are Multi-Release Modular JARs?
Multi-release JARs (introduced in Java 9) let you package version-specific class files in a single JAR. When combined with the module system, you get a modular multi-release JAR—one artifact that works across Java versions and participates in the module graph.
Think of it like a Swiss Army knife: the handle (module descriptor) stays consistent, but different tools (version-specific implementations) deploy based on the Java version running your code.
How It Works
A multi-release modular JAR contains:
- A standard
module-info.classat the root - Base classes for compatibility
- Version-specific classes in
META-INF/versions/<version>/directories - Optionally, version-specific
module-info.classfiles
The directory structure looks like:
mymodule.jar
├── module-info.class (base module descriptor)
├── com/example/MyClass.class (base implementation)
├── META-INF/
│ ├── MANIFEST.MF (Multi-Release: true)
│ └── versions/
│ ├── 11/
│ │ └── com/example/MyClass.class (Java 11+ version)
│ └── 17/
│ ├── module-info.class (Java 17+ module descriptor)
│ └── com/example/MyClass.class (Java 17+ version)
The JVM automatically selects the appropriate version at runtime. Your module can use newer APIs in version-specific directories while maintaining backward compatibility with the base classes.
Why Use This?
You ship one JAR that runs optimally on Java 11, 17, 21+, etc., without forcing users to upgrade or maintaining separate artifacts. The module system ensures strong encapsulation regardless of which version loads.
Key Takeaway: Multi-release modular JARs let you package version-specific optimizations and module descriptors in a single artifact, combining JPMS encapsulation with cross-version compatibility.