All Articles

Dotenv for Java

Dvelopers often grapple with resource configuration, managing indpendent settings for environments such as dev, staging, production, and local development. There are numerous libraries and frameworks that employ vastly different configuration mechanism, sometimes relying on property files, YAML, JSON, INI, TOML, and more. Fortunately, a common mechanism exists that supported pervasevly across most platforms, environment variables.

Enter dotenv-java, a widely adopted solution originating from the Ruby community. dotenv provides a consistent and straightforward method for configuring applications using environment variables, aligning with the principles of the twelve-factor methodology.

A dotenv library exists for most prominent languages including Javascript, Go, Rust, Python, and of course Ruby. Dotenv simplifies the process by loading variables by using simple .env file at startup. the contents of the .env file are simply key value pairs, where the key represnts the name of the environment variable name and the value, its value. The key, value pair in a .env file will never override the actual environment. It’s main purpose is to simplify development, seamlessly simulating a deployment environmemt, but using settings that make sense of local development.

By using environment variables, a robust widely adopted mechanis for managing configuration, Dotenv ensures a uniform configuration experience across development, testing, staging, and production environments, achieved by populating a .env (typical for local development) or leveraging environment variables (typical of deployed environments).

Dotenv describes itself as such (from bkeepers original Ruby dotenv)

Storing configuration in the environment is one of the tenets of a twelve-factor 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.

But it is not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. dotenv loads variables from a .env file into ENV when the environment is bootstrapped.

Here a brief list of dotenv implementations for common language:

This article focuses on the dotenv-kotlin

Getting Started with java-dotenv: Create a .env file in your project’s root:

Use dotenv.get("...") instead of Java’s System.getenv(...). Here’s why.

Create a .env file in the root of your project

# formatted as key=value
MY_ENV_VAR1=some_value
MY_EVV_VAR2=some_value #some value comment
Dotenv dotenv = Dotenv.load();
dotenv.get("MY_ENV_VAR1")

Iterate over environment variables

Note, environment variables specified in the host environment take precedence over those in .env.

for (DotenvEntry e : dotenv.entries()) {
    System.out.println(e.getKey());
    System.out.println(e.getValue());
}

For advanced features, see java-dotenv. If you like what you see, star it on github

Happy coding!

Published Mar 1, 2023

engineer, musician, always learning