Java 9 Modules: Do We Really Need This?
Java 9 finally shipped last week after multiple delays. The headline feature is the module system (Project Jigsaw). I’ve spent the past few days trying to understand if we actually need this.
What Are Modules?
Modules are a way to organize code above the package level. You define a module-info.java file that declares:
- What packages your module exports
- What modules it depends on
module com.example.myapp {
requires java.sql;
requires java.logging;
exports com.example.myapp.api;
}
The idea is to have better encapsulation and explicit dependencies.
The Problem It Solves
1. JAR Hell
Before modules, all public classes were accessible. You could accidentally use internal APIs. Modules let you hide implementation details.
2. Dependency Management
Maven/Gradle manage dependencies, but the JVM doesn’t know about them. Modules make dependencies explicit at the JVM level.
3. JDK Size
The JDK is huge (200+ MB). Modules let you create smaller runtime images with only the modules you need.
The Reality
It’s Complicated
Here’s what I had to do to modularize a simple app:
- Add
module-info.javato each module - Figure out which JDK modules I need (there are 90+ of them)
- Deal with libraries that aren’t modularized yet
- Fix split packages (same package in multiple JARs)
- Update build scripts
Took me a full day for a project with 5 modules.
Most Libraries Aren’t Ready
Popular libraries like Spring, Hibernate, and Jackson aren’t fully modularized yet. You can use them, but you lose some benefits.
The Unnamed Module
Non-modular JARs go into the “unnamed module”. They can access everything, defeating the purpose of encapsulation.
Do You Need Modules?
Probably not, unless:
- You’re building a large application with many modules
- You need to create custom JRE images
- You want strict encapsulation
For most projects:
- Maven/Gradle dependency management is enough
- The added complexity isn’t worth it
- Wait for the ecosystem to catch up
What I’m Doing
Sticking with Java 8 for production. Java 9 modules are too new, and the ecosystem isn’t ready. Maybe in a year or two.
For new projects, I’ll keep an eye on module adoption but won’t force it.
Other Java 9 Features I Like
JShell
Finally, a REPL for Java! Great for quick experiments.
$ jshell
jshell> int x = 10
x ==> 10
jshell> x * 2
$2 ==> 20
Collection Factory Methods
Creating immutable collections is easier:
// Before
List<String> list = Collections.unmodifiableList(
Arrays.asList("a", "b", "c")
);
// Java 9
List<String> list = List.of("a", "b", "c");
Try-With-Resources Improvements
Can use effectively final variables:
BufferedReader reader = new BufferedReader(...);
try (reader) { // No need to redeclare
// Use reader
}
The Verdict
Java 9 modules are interesting but not essential for most developers. The other features (JShell, collection factories, etc.) are nice quality-of-life improvements.
If you’re on Java 8, there’s no rush to upgrade. Wait for Java 10 or 11, and let the ecosystem mature.
Anyone using Java 9 modules in production? How’s it going?