Validating Module Boundaries with jdeps
You'll learn how to use the jdeps command-line tool to analyze your module dependencies, catch violations, and confirm that your module structure is sound.
What Is jdeps?
jdeps (Java Dependencies) is a static analysis tool shipped with the JDK that inspects your compiled classes and JARs to reveal their dependencies. In the context of JPMS, it's invaluable for verifying that your modules follow their declared contracts—ensuring that code only accesses packages that are properly exportsed or opensed, and that your requires declarations match actual usage.
Think of jdeps as a code inspector that walks through your building (module) and checks whether all the doors (dependencies) between rooms (packages) are officially allowed and properly labeled.
Basic Usage
To analyze a module's dependencies:
jdeps --module-path <path> --module <module-name>
This shows which packages your module uses and from which modules they come.
Detecting Violations
To specifically check for encapsulation violations—where your code tries to access non-exported or non-opened packages:
jdeps --check <module-name>
This reports any illegal accesses that would fail at runtime due to strong encapsulation.
Verifying Module Structure
Use jdeps to generate a module dependency summary:
jdeps --print-module-deps <jar-file>
This outputs a comma-separated list of required platform modules, helping you minimize your requires declarations to only what's truly needed.
Common Workflow
- Run
jdeps --checkto find violations - Review reported illegal accesses
- Update
module-info.javawith appropriateexports,opens, orrequires - Re-run
jdepsto confirm fixes
Key Takeaway: jdeps is your essential verification tool for modular Java—use it to audit dependencies, detect encapsulation breaches, and ensure your module-info.java accurately reflects your code's actual needs.