Kayavak / The Backend / BE·42 Modules, Packaging and Dependency Conflicts / Deeper
Deeper · The Backend
Modules, Packaging and Dependency Conflicts
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
Java's classpath was simple: a list of JARs, searched in order, first match wins. As applications grew to hundreds of
libraries, two versions of the same library would both end up on the classpath, and which one loaded depended on order:
"JAR hell", with NoSuchMethodError appearing only when a rarely used path ran. OSGi (from 1999–2000) answered with a
module system where each bundle declares what it imports and exports and can have its own versions, powerful and
complex. Java's own module system (Project Jigsaw, Java 9, 2017) added strong encapsulation and reliable
configuration, but not multiple versions of one module.
Dependency resolution itself is a design choice. Maven picks the version nearest to your project in the dependency tree; Gradle picks the highest requested; Go (2018, Russ Cox) chose minimal version selection: the oldest version that satisfies every requirement, making builds reproducible without a lockfile. Shading, BOMs and platforms are the tools for living with conflicts the resolver can't fix.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
At run time there's one class per name per class loader. Every dependency conflict is two libraries expecting different versions of a class, and the build deciding, often silently, which one they both get.
| Question | Answer |
|---|---|
| What is it? | How Java code is packaged into artifacts, found at run time, and how version conflicts are resolved |
| Problem it solves | Assembling hundreds of independently versioned libraries into one working program |
| Parts | JARs and coordinates, repositories, transitive dependencies, resolution strategies, class loaders, the module system, shading |
| How it works | The build resolves a graph to one version per artifact; the class loader then finds each class on the path |
| Assumptions | Libraries stay compatible across the versions the resolver picks |
| When they change | A transitive dependency upgraded by another library removes a method yours calls: NoSuchMethodError at run time |
| Where it fails | Split packages, duplicate classes in different JARs, libraries that need incompatible versions |
| Related ideas | Linking (Foundations, ch10); build systems (ch43); supply chain (Security) |
What if?
What if two libraries need Guava 20 and Guava 33 respectively?
Reason it through, then open
Only one Guava can be on the classpath. The resolver picks one (nearest or highest), and whichever library expected the other version may fail at the first call to a method that was added, removed or changed. Options: find versions of the libraries that agree, shade (relocate) one library's copy of Guava into its own package, or isolate them in separate class loaders.
What if Maven picked the highest version instead of the nearest?
Reason it through, then open
Libraries would more often get versions at least as new as they asked for, which usually works because most libraries stay backward compatible, but a library asking for an older major version could break. "Nearest wins" lets you fix any conflict by declaring the version you want directly in your own POM, since your declaration is nearest of all. Neither rule is safe without tests; they're different defaults.
Where the boundary is
| Aspect | Classpath | Module path |
|---|---|---|
| Unit | JARs, unordered contents | Named modules with requires and exports |
| Encapsulation | None: every public class is reachable | Only exported packages are reachable |
| Missing dependency found | At run time, when used | At start-up |
| Split packages | Allowed, silently | Rejected |
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| Maven resolves conflicts by nearest definition | Convention | Maven's documented mediation rule |
| Java modules shipped in Java 9 (2017) | Historical | JEP 261 |
| Minimal version selection gives reproducible builds without a lockfile | Theorem, within Go's rules | Russ Cox's design (2018) |
Still open
How to manage transitive dependencies safely (each one is code you run) is an open supply-chain problem: SBOMs, signed artifacts and provenance are being adopted unevenly.
What would change your mind? If libraries kept perfect backward compatibility, resolution strategy wouldn't matter. Semantic versioning promises it; ecosystems don't always deliver.
Recall
What causes NoSuchMethodError at run time?
A class compiled against one version of a library running with another that lacks the method.
How does Maven pick between conflicting versions?
The nearest declaration to your project in the dependency tree wins.
What does shading do?
Copies a dependency into your artifact under a relocated package so it can't conflict.
What does the module path add over the classpath?
Declared dependencies checked at start-up and encapsulation of non-exported packages.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.