Split Packages: The Problem JPMS Solves
What you'll learn: You'll understand how the classpath allows the same package to exist in multiple JARs (split packages) and why the module path enforces a strict prohibition against this problematic pattern.
The Classpath Problem: Split Packages
On the classpath, Java allows the same package to be "split" across multiple JAR files. For example, you could have com.example.util classes in both library-a.jar and library-b.jar. The JVM simply merges these at runtime, searching all JARs for classes in that package.
Why is this dangerous?
- Hidden conflicts: If both JARs contain
com.example.util.Helper, only one version loads—whichever the classloader finds first. The other silently disappears. - Unpredictable behavior: Class visibility depends on JAR ordering, leading to "works on my machine" bugs.
- Maintenance nightmare: Developers can't tell which JAR owns which classes in a package.
Analogy: Imagine two filing cabinets both labeled "2023 Invoices." When you need a document, you check one cabinet first, and if you find any folder matching your search, you stop looking—even though the second cabinet might have newer versions of the same files.
How JPMS Solves This
The module path enforces a strict rule: each package must belong to exactly one module. When you use exports com.example.util in module-info.java, you're declaring ownership. If another module tries to export the same package, compilation or launch fails immediately with a clear error.
This prevents split packages entirely, forcing clean boundaries and explicit dependencies through requires directives.
Key Takeaway: The classpath permits split packages—allowing the same package across multiple JARs—which causes hidden conflicts and unpredictable behavior; JPMS eliminates this anti-pattern by requiring each package to belong to exactly one module.