{"id":15590259,"url":"https://github.com/cdimascio/dotenv-java","last_synced_at":"2025-05-14T14:08:25.553Z","repository":{"id":39619416,"uuid":"296778170","full_name":"cdimascio/dotenv-java","owner":"cdimascio","description":"🗝️ Dotenv is a no-dep, pure Java module that loads environment variables from a .env file","archived":false,"fork":false,"pushed_at":"2025-02-24T01:08:47.000Z","size":775,"stargazers_count":530,"open_issues_count":7,"forks_count":45,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-12T14:53:35.726Z","etag":null,"topics":["12-factor","android","dotenv","environment-configuration","environment-variables","hacktoberfest","java"],"latest_commit_sha":null,"homepage":"https://github.com/cdimascio/dotenv-java","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cdimascio.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":"https://www.buymeacoffee.com/m97tA5c"}},"created_at":"2020-09-19T03:24:43.000Z","updated_at":"2025-04-11T02:47:54.000Z","dependencies_parsed_at":"2024-12-13T01:00:46.423Z","dependency_job_id":"b28b5ca3-68ab-46af-9c6d-3d33259ff834","html_url":"https://github.com/cdimascio/dotenv-java","commit_stats":{"total_commits":132,"total_committers":14,"mean_commits":9.428571428571429,"dds":"0.19696969696969702","last_synced_commit":"f9989e14d4c1b09026e0a69144387ca196a0d688"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdimascio%2Fdotenv-java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdimascio%2Fdotenv-java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdimascio%2Fdotenv-java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdimascio%2Fdotenv-java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cdimascio","download_url":"https://codeload.github.com/cdimascio/dotenv-java/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254159771,"owners_count":22024564,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["12-factor","android","dotenv","environment-configuration","environment-variables","hacktoberfest","java"],"created_at":"2024-10-02T23:20:58.404Z","updated_at":"2025-05-14T14:08:25.535Z","avatar_url":"https://github.com/cdimascio.png","language":"Java","readme":"# 🗝️ dotenv-java \n\n![Build Status](https://github.com/cloudsimplus/cloudsimplus/actions/workflows/build.yml/badge.svg) [![Maven Central](https://img.shields.io/maven-central/v/io.github.cdimascio/dotenv-java.svg?label=Maven%20Central)](https://search.maven.org/artifact/io.github.cdimascio/dotenv-java) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/66b8195f0da544f1ad9ed1352c0ea66f)](https://app.codacy.com/app/cdimascio/dotenv-java?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=cdimascio/dotenv-java\u0026utm_campaign=Badge_Grade_Dashboard) ![](https://img.shields.io/ossf-scorecard/github.com/cdimascio/dotenv-java?label=openssf%20scorecard\u0026style=flat) [![OpenSSF Best Practices](https://www.bestpractices.dev/projects/9407/badge)](https://www.bestpractices.dev/projects/9407) [![](https://img.shields.io/gitter/room/cdimascio-oss/community?color=%23eb205a)](https://gitter.im/cdimascio-oss/community) [![All Contributors](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors-) [![](https://img.shields.io/badge/doc-javadoc-blue)](https://cdimascio.github.io/dotenv-java/docs/javadoc/index.html) ![](https://img.shields.io/badge/license-Apache%202.0-blue.svg)\n\nA no-dependency, pure Java port of the Ruby dotenv project. Load environment variables from a `.env` file.\n\n\u003cp align=\"center\"\u003e\n\t\u003cimg src=\"https://raw.githubusercontent.com/cdimascio/dotenv-java/master/assets/dotenv-java-logo.png\" alt=\"dotenv\" width=\"550\"/\u003e \n\u003c/p\u003e\n\n_dotenv-java also powers the popular [dotenv-kotlin](https://github.com/cdimascio/dotenv-kotlin) library._\n\nWhy dotenv?\n\n\u003eStoring configuration in the environment is one of the tenets of a [twelve-factor](http://12factor.net/config) app. Anything that is likely to change between deployment environments–such as resource handles for databases or credentials for external services–should be extracted from the code into environment variables.\n\n\u003eBut it is not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. Dotenv load variables from a .env file into ENV when the environment is bootstrapped.\n\n\u003e-- [Brandon Keepers](https://github.com/bkeepers/dotenv)\n\n\nEnvironment variables listed in the host environment override those in `.env`.  \n\nUse `dotenv.get(\"...\")` instead of Java's `System.getenv(...)`.  \n\n## Install\n\n_Requires Java 11 or greater._\n\n_Still using Java 8? Use version 2.3.2_\n\n### Maven\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.github.cdimascio\u003c/groupId\u003e\n    \u003cartifactId\u003edotenv-java\u003c/artifactId\u003e\n    \u003cversion\u003e3.2.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### Gradle \u003c4.10\n\n```groovy\ncompile 'io.github.cdimascio:dotenv-java:3.2.0'\n```\n\n### Gradle \u003e=4.10\n\n```groovy\nimplementation 'io.github.cdimascio:dotenv-java:3.2.0'\n```\n\n### Gradle Kotlin DSL\n\n```kotlin\nimplementation(\"io.github.cdimascio:dotenv-java:3.2.0\")\n```\n\nLooking for the Kotlin variant? **get [dotenv-kotlin](https://github.com/cdimascio/dotenv-kotlin)**.\n\n## Usage\nUse `dotenv.get(\"...\")` instead of Java's `System.getenv(...)`. Here's [why](#faq).\n\nCreate a `.env` file in the root of your project\n\n```dosini\n# formatted as key=value\nMY_ENV_VAR1=some_value\nMY_EVV_VAR2=some_value #some value comment\nMULTI_LINE=\"some\nmulti line\nvalue\"\n```\n\n\n```java\nDotenv dotenv = Dotenv.load();\ndotenv.get(\"MY_ENV_VAR1\")\n```\n\n## Android Usage\n\n- Create an assets folder\n- Add `env` *(no dot)* to the assets folder\n\n\t\u003cimg src=\"assets/android-dotenv.png\" width=\"350\"\u003e\n\n- Configure dotenv to search `/assets` for a file with name `env`\n\n\t```java\n    Dotenv dotenv = Dotenv.configure()\n       .directory(\"/assets\")\n       .filename(\"env\") // instead of '.env', use 'env'\n       .load();\n\n\tdotenv.get(\"MY_ENV_VAR1\");\n\t```\n\n**Note:** The above configuration is required because dot files in `/assets` do not appear to resolve on Android. *(Seeking recommendations from the Android community on how `dotenv-java` configuration should work in order to provide the best experience for Android developers)*\n\nAlternatively, if you are using Provider `android.resource` you may specify\n\n```\n .directory(\"android.resource://com.example.dimascio.myapp/raw\")\n```\n\n## Advanced Usage\n\n### Configure\nConfigure `dotenv-java` once in your application. \n\n```java\nDotenv dotenv = Dotenv.configure()\n        .directory(\"./some/path\")\n        .ignoreIfMalformed()\n        .ignoreIfMissing()\n        .load();\n```\n\n- see [configuration options](#configuration-options)\n\n### Get environment variables\nNote, environment variables specified in the host environment take precedence over those in `.env`.\n\n```java\ndotenv.get(\"HOME\");\ndotenv.get(\"MY_ENV_VAR1\", \"default value\");\n```\n\n### Iterate over environment variables\nNote, environment variables specified in the host environment take precedence over those in `.env`.\n\n```java\nfor (DotenvEntry e : dotenv.entries()) {\n    System.out.println(e.getKey());\n    System.out.println(e.getValue());\n}\n```\n\n## Configuration options\n\n### *optional* `directory` \n* `path` specifies the directory containing `.env`. Dotenv first searches for `.env` using the given path on the filesystem. If not found, it searches the given path on the classpath. If `directory` is not specified it defaults to searching the current working directory on the filesystem. If not found, it searches the current directory on the classpath.\n\n\t**example**\n\t\n\t```java\n\tDotenv\n\t  .configure()\n\t  .directory(\"/some/path\")\n\t  .load();\n\t```\n\n### *optional* `filename`\n\n* Use a filename other than `.env`. Recommended for use with Android (see [details](#android-usage)) \n\n\t**example**\n\t\n\t```java\n\tDotenv\n\t  .configure()\n\t  .filename(\"myenv\")\n\t  .load();\n\t```\n\n### *optional* `ignoreIfMalformed`\n\n* Do not throw when `.env` entries are malformed. Malformed entries are skipped.\n\n\t**example**\n\t\n\t```java\n\tDotenv\n\t  .configure()\n\t  .ignoreIfMalformed()\n\t  .load();\n\t```\n\n### *optional* `ignoreIfMissing` \n\n* Do not throw when `.env` does not exist. Dotenv will continue to retrieve environment variables that are set in the environment e.g. `dotenv[\"HOME\"]`\n\n\t**example**\n\t\n\t```java\n\tDotenv\n\t  .configure()\n\t  .ignoreIfMissing()\n\t  .load();\n\t```\n\n### *optional* `systemProperties` \n\n* Load environment variables into System properties, thus making all environment variables accessible via `System.getProperty(...)`\n\n\t**example**\n\t\n\t```java\n\tDotenv\n\t  .configure()\n\t  .systemProperties()\n\t  .load();\n\t```\n\n## Examples\n- with [Maven (simple)](examples/maven-simple)\n- see [Java tests](./src/test/java/tests/DotenvTests.java) \n\n## Powered by dotenv-java\n- [spring-dotenv](https://github.com/paulschwarz/spring-dotenv) - dotenv-java as a Spring PropertySource\n- [dotenv-kotlin](https://github.com/cdimascio/dotenv-kotlin) - a Kotlin DSL for dotenv-java\n\n## [Javadoc](https://cdimascio.github.io/dotenv-java/docs/javadoc/)\n\nsee [javadoc](https://cdimascio.github.io/dotenv-java/docs/javadoc/)\n\n## FAQ\n\n**Q:** Should I deploy a `.env` to e.g. production?\n\n**A**: Tenet III of the [12 factor app methodology](https://12factor.net/config) states \"The twelve-factor app stores config in environment variables\". Thus, it is not recommended to provide the .env file to such environments. dotenv, however, is super useful in e.g a local development environment as it enables a developer to manage the environment via a file which is more convenient.\n\nUsing dotenv in production would be cheating. This type of usage, however is an anti-pattern.\n\n**Q:** Why should I use `dotenv.get(\"MY_ENV_VAR\")` instead of `System.getenv(\"MY_ENV_VAR\")`\n\n**A**: Since Java does not provide a way to set environment variables on a currently running process, vars listed in `.env` cannot be set and thus cannot be retrieved using `System.getenv(...)`.\n\n**Q**: Can I use `System.getProperty(...)` to retrieve environment variables?\n\n**A**: Sure. Use the `systemProperties` option. Or after initializing dotenv set each env var into system properties manually. For example:\n\nexample\n```java\nDotenv dotenv = Dotenv.configure().load();\ndotenv.entries().forEach(e -\u003e System.setProperty(e.getKey(), e.getValue()));\nSystem.getProperty(\"MY_VAR\");\n```\n\n**Q:** Should I have multiple .env files?\n\n**A**: No. We strongly recommend against having a \"main\" .env file and an \"environment\" .env file like .env.test. Your config should vary between deploys, and you should not be sharing values between environments.\n\n\u003e In a twelve-factor app, env vars are granular controls, each fully orthogonal to other env vars. They are never grouped together as “environments”, but instead are independently managed for each deploy. This is a model that scales up smoothly as the app naturally expands into more deploys over its lifetime.\n\n\u003e– The Twelve-Factor App\n\n**Q**: Should I commit my `.env` file?\n\n**A**: No. We strongly recommend against committing your `.env` file to version control. It should only include environment-specific values such as database passwords or API keys. Your production database should have a different password than your development database.\n\n**Q**: What happens to environment variables that were already set?\n\n**A**: dotenv-java will never modify any environment variables that have already been set. In particular, if there is a variable in your `.env` file which collides with one that already exists in your environment, then that variable will be skipped. This behavior allows you to override all `.env` configurations with a machine-specific environment, although it is not recommended.\n\n**Q**: What about variable expansion in `.env`? \n\n**A**: We haven't been presented with a compelling use case for expanding variables and believe it leads to env vars that are not \"fully orthogonal\" as [The Twelve-Factor App outlines](https://12factor.net/config). Please open an issue if you have a compelling use case.\n\n**Q**: Can I supply a multi-line value?\n\n**A**: dotenv-java exhibits the same behavior as Java's `System.getenv(...)`, thus if a multi-line value is needed you might consider encoding it via e.g. Base64. see this [comment](https://github.com/cdimascio/dotenv-java/issues/28#issuecomment-489443975) for details.\n\n\n**Note and reference**: The FAQs present on [motdotla's dotenv](https://github.com/motdotla/dotenv#faq) node project page are so well done that I've included those that are relevant in the FAQs above.\n\nsee [CONTRIBUTING.md](CONTRIBUTING.md)\n\n## License\n\nsee [LICENSE](LICENSE) ([Apache 2.0](LICENSE))\n\n\u003ca href=\"https://www.buymeacoffee.com/m97tA5c\" target=\"_blank\"\u003e\u003cimg src=\"https://bmc-cdn.nyc3.digitaloceanspaces.com/BMC-button-images/custom_images/orange_img.png\" alt=\"Buy Me A Coffee\" style=\"height: auto !important;width: auto !important;\" \u003e\u003c/a\u003e\n\n## Contributors ✨\n\nThanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n\u003c!-- prettier-ignore-start --\u003e\n\u003c!-- markdownlint-disable --\u003e\n\u003ctable\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://github.com/Mooninaut\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/1463364?v=4?s=100\" width=\"100px;\" alt=\"Clement Cherlin\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eClement Cherlin\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/cdimascio/dotenv-java/commits?author=Mooninaut\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"https://github.com/cdimascio/dotenv-java/commits?author=Mooninaut\" title=\"Tests\"\u003e⚠️\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://github.com/alexbraga\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/61568124?v=4?s=100\" width=\"100px;\" alt=\"Alex Braga\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eAlex Braga\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/cdimascio/dotenv-java/commits?author=alexbraga\" title=\"Documentation\"\u003e📖\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://github.com/c00ler\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/1210272?v=4?s=100\" width=\"100px;\" alt=\"Alexey Venderov\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eAlexey Venderov\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/cdimascio/dotenv-java/commits?author=c00ler\" title=\"Code\"\u003e💻\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://github.com/yassenb\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/531223?v=4?s=100\" width=\"100px;\" alt=\"yassenb\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eyassenb\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/cdimascio/dotenv-java/commits?author=yassenb\" title=\"Code\"\u003e💻\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://sidney.beekhoven.nl\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/903673?v=4?s=100\" width=\"100px;\" alt=\"Sidney Beekhoven\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eSidney Beekhoven\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/cdimascio/dotenv-java/commits?author=dizney\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"https://github.com/cdimascio/dotenv-java/commits?author=dizney\" title=\"Documentation\"\u003e📖\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://youtube.com/@manoelcamposdev\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/261605?v=4?s=100\" width=\"100px;\" alt=\"Manoel Campos\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eManoel Campos\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/cdimascio/dotenv-java/commits?author=manoelcampos\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"https://github.com/cdimascio/dotenv-java/commits?author=manoelcampos\" title=\"Tests\"\u003e⚠️\u003c/a\u003e \u003ca href=\"#infra-manoelcampos\" title=\"Infrastructure (Hosting, Build-Tools, etc)\"\u003e🚇\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\n\u003c!-- markdownlint-restore --\u003e\n\u003c!-- prettier-ignore-end --\u003e\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\nThis project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!\n","funding_links":["https://www.buymeacoffee.com/m97tA5c"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcdimascio%2Fdotenv-java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcdimascio%2Fdotenv-java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcdimascio%2Fdotenv-java/lists"}