<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/optional-dependencies-with-requires-static-sahsy8igv" -->

---
title: Optional Dependencies with requires static | daily.dev
description: 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...
canonical: https://daily.dev/posts/optional-dependencies-with-requires-static-sahsy8igv
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Optional Dependencies with requires static | daily.dev
og:description: 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...
og:url: https://daily.dev/posts/optional-dependencies-with-requires-static-sahsy8igv
og:image: https://api.daily.dev/og/posts/saHsY8igV.png
og:image:alt: Optional Dependencies with requires static
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Optional Dependencies with requires static

**[nipafx \(Nicolai Parlog\)](https://daily.dev/sources/nipafx)** · 10 min read · 0 upvotes · 0 comments

## Summary

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.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://nipafx.dev/java-modules-optional-dependencies>

## Similar posts on daily.dev

- [Compile-Time Conditions in Java](https://daily.dev/posts/compile-time-conditions-in-java-kjy5rh792) · Baeldung · 0 upvotes · 0 comments
- [Microservice Dependencies: How @requires Makes Them Explicit](https://daily.dev/posts/microservice-dependencies-how-requires-makes-them-explicit-hejzg4lfe) · WunderGraph · 1 upvotes · 0 comments
- [JMOD File Format in Java](https://daily.dev/posts/jmod-file-format-in-java-ts6pnbcrc) · Baeldung · 11 upvotes · 2 comments

---

Tags: [#java](https://daily.dev/tags/java)

[View this post on daily.dev](https://daily.dev/posts/optional-dependencies-with-requires-static-sahsy8igv)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Optional Dependencies with requires static","url":"https://daily.dev/posts/optional-dependencies-with-requires-static-sahsy8igv","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/optional-dependencies-with-requires-static-sahsy8igv"},"datePublished":"2026-03-16T08:14:07.041Z","dateModified":"2026-03-16T08:25:01.257Z","description":"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...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/7fc589b3c425ab9d5756da8ec504e847?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/7fc589b3c425ab9d5756da8ec504e847?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"nipafx (Nicolai Parlog)","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"nipafx (Nicolai Parlog)","logo":"https://media.daily.dev/image/upload/s--H_Echo_u--/f_auto,q_auto/v1773648772/logos/nipafx?_a=BAMAMiiu0","url":"https://daily.dev/sources/nipafx"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/optional-dependencies-with-requires-static-sahsy8igv","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"java","timeRequired":"PT10M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"nipafx (Nicolai Parlog)","item":"https://daily.dev/sources/nipafx"},{"@type":"ListItem","position":3,"name":"Optional Dependencies with requires static"}]}
```

