1. Problems with the old approach
“Classpath party”: when everyone is in the same kitchen
Before modules (in Java 9), all application code, libraries, and dependencies were dumped into one big heap — the classpath. There were effectively no boundaries between libraries: any class that landed on the classpath could be found and used by anyone.
Name conflicts would appear: two libraries contain a class with the same fully qualified name, for example com.example.Util — “some one” would be picked, and you’d only discover the problem from strange runtime behavior.
A classic of the genre is dependency hell: one library requires a logger version 1.2, another — 1.3, and both end up on the classpath. The JVM doesn’t know which to pick up, and you get a mysterious NoSuchMethodError.
Also: even if a class is declared public but is intended for a library’s internal use, other code can still use it — and break something in the process. Large projects turned into a minefield, and maintenance — into an adventure.
2. What is a module in Java?
A module: like a box with holes
Java 9 introduced a module system (JPMS). A module is a box with classes and packages where you make “holes” yourself: you explicitly state what you show to the outside (exports) and what you hide inside. And yes: even a public class isn’t visible to other modules if its package isn’t exported.
Formal definition
A module is a logical unit of code organization that groups related packages and classes. Each module explicitly declares:
- what it exports — makes available to other modules (exports);
- and what it imports — what other modules it depends on (requires).
Key properties of a module:
- Explicit boundaries. It’s clearly defined what is accessible from outside and what is not.
- Explicit dependencies. A module doesn’t “see” another module without an explicit requires.
- Isolation. Internal details can be completely hidden from the outside world.
3. Advantages of modularity
Improved encapsulation
There’s a new visibility level — at the module level. Even if a class is public, if its package isn’t exported, it’s visible only inside the module. A library’s internals no longer “leak” out.
Explicit dependency description
Required dependencies are listed in module-info.java. The compiler and IDE will warn you in advance if you forgot to specify a module you depend on.
Improved security and reliability
Restricting access to internal APIs makes accidental or deliberate interference harder. This is critical for large libraries and platform modules.
Ability to create “stripped-down” JREs
With jlink you can build a minimal runtime from only the modules you need. In the cloud and embedded scenarios this saves disk space and memory.
Bonus: faster startup and smaller size
Not all modules are loaded — only those actually used — so applications start faster and consume fewer resources.
4. Where modules are used
In the Java standard library
Starting with Java 9, the platform itself is modular. Examples:
- java.base — the base module (required for any program).
- java.sql — working with databases.
- java.xml — working with XML.
If you don’t use XML, the java.xml module won’t even make it into your runtime.
In large applications and libraries
A must-have for enterprise systems where dozens of teams develop parts of a monorepo. Modularity reduces conflicts and simplifies maintenance.
In your own projects
Even in a pet project, modules help train architectural thinking and keep code tidy, avoiding spaghetti.
5. Quick syntax overview: module-info.java
The most important thing — the module-info.java file
The module-info.java file lives at the root of a module’s sources and declares its boundaries and dependencies.
module my.awesome.module {
exports com.example.api; // Exported package
requires java.sql; // Dependency on a standard module
}
Main keywords:
- module <name> — declares a module.
- exports <package> — makes a package available to other modules.
- requires <module> — declares a dependency on another module.
Minimal example:
module com.myproject.core {
exports com.myproject.core.api;
}
Example with a dependency:
module com.myproject.app {
requires com.myproject.core;
requires java.sql;
}
Additional features (briefly): for reflection there is opens, for services — uses and provides ... with ... (details — in advanced lectures).
6. Useful nuances
Modules are like “passports” for classes. Previously, any class with a public visa could “travel” across the application. Now you also need a module “passport” — a package export (exports). Without it, the class stays “at home.”
Dependency hell is a real term. If you’ve seen ClassNotFoundException: com.google.common.base.Strings, you’ve been there. Modules are meant to banish these “demons” with strict boundaries and dependencies.
All of Java is now modular. Even if you don’t write your own modules, the platform is already split into dozens. Try the command:
java --list-modules
How this affects development
You explicitly control code visibility, and internal packages no longer become accessible to everyone “by accident.” The IDE and compiler catch errors earlier: forget to declare a dependency — the project won’t build. Maintaining large systems becomes easier: it’s simpler to understand who depends on whom and what will break when things change.
7. Common mistakes when migrating to modules
Error #1: you forgot to export a package, but the class is public. You declared a class as public but didn’t export its package — other modules won’t be able to use it. The compiler will report: “Package is not exported by the module.”
Error #2: you didn’t declare a dependency via requires. You use a type from another module but forgot to add requires to module-info.java. Result — a compilation error: “the module cannot find the required class.”
Error #3: duplicate module names. In a large project, two modules accidentally received the same name. The JVM won’t tolerate that — rename them and stick to a consistent naming scheme.
Error #4: you forgot about standard modules. For example, you use JDBC but didn’t add requires java.sql;. In Java 8 “it was visible anyway,” but in 9+ — it isn’t.
Error #5: attempting to use another module’s internal class. If a package isn’t exported, even a public class remains invisible from the outside. Export the package or move the API into a “public” layer.
GO TO FULL VERSION