Encrypt a .env file in Java

Encrypt a .env file in Java with dotenvx, commit it safely, and inject its secrets at runtime.

1. Install

Get the Dotenvx CLI.

$ curl -sfS https://dotenvx.sh | sh
◈ installed (dotenvx)

more install options

2. Encrypt

Encrypt your .env file.

$ dotenvx encrypt
◈ encrypted (.env)

3. Inject

Then inject your encrypted secrets at runtime with dotenvx run.

index.java

public class Index {
  public static void main(String[] args) {
    String hello = System.getenv("HELLO");
    System.out.println("Hello " + hello);
  }
}
dotenvx run -- java index.java