Package Splitting and exports: Avoiding Conflicts
You'll learn: Why JPMS strictly forbids splitting a single package across multiple modules and how to recognize this restriction.
The One-Module-Per-Package Rule
In the Java Platform Module System, each package must belong to exactly one module. This is called the "no split packages" rule. You cannot have com.example.util in both module.a and module.b—even if they export different classes from that package.
This is a hard constraint enforced at module resolution time. If the module system detects the same package in two different modules on the module path, it will fail with an error before your application even starts.
Why This Matters
Before JPMS, the classpath allowed the same package to appear in multiple JARs. This caused:
- Shadowing: One JAR's class could accidentally hide another's
- Versioning conflicts: Two libraries shipping different versions of
org.apache.commons.lang - Unpredictable behavior: Class loading order determined which version you got
JPMS eliminates this ambiguity by design. If you own com.myapp.data, only one module can contain types in that package.
What Happens If You Try?
// In module.a
exports com.example.shared;
// In module.b — THIS IS ILLEGAL
exports com.example.shared; // ❌ Error at module resolution
The JVM will reject this configuration immediately with a message like:
"Package com.example.shared is declared in modules module.a and module.b"
Design Around It
Use distinct package names per module. Instead of splitting com.example.util, create:
com.example.core.utilin your core modulecom.example.plugin.utilin your plugin module
Key Takeaway:
JPMS enforces that every package belongs to exactly one module—split packages are forbidden, preventing classpath-era shadowing and version conflicts.