Module Graph and Dependencies at Runtime
You'll learn how the Java module system builds and resolves a directed graph of module dependencies when your application starts.
The Core Idea
When you launch a modular Java application, the JVM doesn't just randomly load modules. Instead, it constructs a module graph—a directed network showing which modules depend on which others. Starting from your main module (the root), the module system follows every requires directive in each module-info.java file, recursively traversing dependencies until all reachable modules are identified and validated.
Think of it like mapping a road trip: you start at your home (root module), follow roads (requires statements) to cities (other modules), and those cities have roads leading to even more destinations. The JVM builds this complete map before letting your code execute.
How Resolution Works
- Root identification: The module containing your main class becomes the root
- Recursive traversal: For each module encountered, parse its
module-info.javaand follow itsrequiresdirectives - Graph construction: Build a directed graph where nodes are modules and edges are dependencies
- Validation: Check for cycles, missing modules, split packages, and accessibility violations
- Readability establishment: Only modules connected in the graph can read each other
If any required module is missing or conflicts exist (like two modules exporting the same package), startup fails immediately with a clear error—no more cryptic ClassNotFoundExceptions at runtime!
This is fundamentally different from the classpath, which resolved classes lazily and could fail deep into execution.
Key Takeaway:
The module system builds a complete dependency graph at startup, validating all module relationships before any application code runs, ensuring reliable resolution and strong encapsulation from the very beginning.