Automatic Modules Export All Packages
You'll learn how automatic modules differ from explicit modules by making all their packages publicly accessible.
The Core Difference
When a regular (non-modular) JAR is placed on the module path, it becomes an automatic module—and here's the critical behavior: automatic modules implicitly export every single package they contain. This is fundamentally different from explicit modules, where you must deliberately use the exports directive in module-info.java to make packages visible.
Think of it like moving from a gated community (explicit module) where you control exactly which entrances are open, to a park (automatic module) where all paths are automatically accessible to everyone.
Why This Design Choice?
This all-exporting behavior exists for compatibility. Legacy libraries weren't designed with JPMS encapsulation in mind—they assumed all public classes would be accessible. If automatic modules enforced strong encapsulation, most existing JARs would break when migrated to the module path.
The Trade-off
While this makes migration smoother, it sacrifices the encapsulation benefits that explicit modules provide:
- Explicit module: You control visibility with
exports com.myapp.api; - Automatic module: All packages like
com.legacy.api,com.legacy.internal,com.legacy.utilare automatically exported—no hiding internals
This means if mylib.jar becomes an automatic module, both its intended public API and its internal implementation packages become accessible to modules that requires mylib.
Security Consideration
Remember: automatic modules still respect Java's access modifiers. A private or package-private class won't suddenly become usable—but any public class in any package becomes accessible.
Key Takeaway: Automatic modules export all their packages by default, trading strong encapsulation for backward compatibility with non-modular JARs—a temporary bridge during migration to explicit modules.