question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make dotenv env variables available in System.getProperty() too

See original GitHub issue

Q: Why should I use dotenv.get(“MY_ENV_VAR”) instead of System.getenv(“MY_ENV_VAR”) 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(…).

Given that System.getenv("MY_ENV_VAR") doesn’t allow adding new environment variables at runtime, the other option is to plug the dotenv configured variables into System.setProperty().

This will allow libraries to use either System.getProperty("MY_ENV_VAR") or dotenv.get("MY_ENV_VAR").

A simple change would look like this after Dotenv is created.

Dotenv
   .load()
   .entries()
   .filter(entry -> System.getProperty(entry.getKey) == null)
   .forEach(entry -> System.setProperty(entry.getKey(), entry.getValue()));

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jongiocommented, May 14, 2020
1reaction
jongiocommented, May 13, 2020

Very cool. Thanks for doing this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java System.getProperty vs System.getenv - Baeldung
In this tutorial, we'll explore the differences between two System methods for reading system properties and environment variables.
Read more >
Using env variable in Spring Boot's application.properties
Using Spring context 5.0 I have successfully achieved loading correct property file based on system environment via the following annotation
Read more >
Using dotenv package to create environment variables - Medium
One solution for this is to use environment variables. These are local variables that are made available to an application.
Read more >
24. Externalized Configuration - Spring
Spring Boot allows you to externalize your configuration so you can work with the same application code in different environments. You can use...
Read more >
How to Easily Set-up Node Config (Best Practices)
DO THE DOTENV ... Dotenv is a great Node library that enables the use of a .env file that can hold environment variables...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found