The Java Platform Module System (JPMS) enforces strict dependency rules by default, requiring modules to be present at both compile and run time. The `requires static` clause provides a way to declare optional dependencies that must be present at compile time but can be absent at run time. Two use cases are explored: a utility library integrating with multiple optional third-party libraries, and a module that conditionally uses an advanced statistics library. At run time, optional dependencies are not resolved automatically and must be explicitly added via `--add-modules` or pulled in by another regular dependency. Coding against optional dependencies requires care to avoid `NoClassDefFoundError`; the post demonstrates using the stack-walking API to check whether an optional module is present before accessing its types.
Table of contents
▚ The Conundrum Of Unrequired Dependencies▚ Optional Dependencies With requires static▚ Resolution Of Optional Dependencies▚ Coding Against Optional Dependencies▚ SummarySort: