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.

Making dotenv variables globally available

See original GitHub issue

I was wondering if there was any way to make the variables in the .env file globally available throughout my entire java application.

It seems as though configuring and loading the variables returns a Map of sorts from which you can retrieve all of the values. However I have to configure and load this variable in every file from which I wish to use it.

Dotenv env = Dotenv.configure().directory("./").load();

Is there any way I can load them once and use them everywhere? Perhaps even through the native System.getenv() static method? Running the configuration and loading once from my public static main method or something.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jessevdpcommented, Apr 25, 2018

I build my own little util class if you’re interested. I used this class to normalise the configuration for Dotenv throughout my app too.

import io.github.cdimascio.dotenv.Dotenv;

public class Config {
	private static final Config instance = new Config();
	
	public static Config getConfig () {
		return instance;
	}
	
	private Dotenv env;
			
	public Config () {
		this.env = Dotenv.configure().directory("./").ignoreIfMissing().load();
	}
	
	public String get (String variable) {
		return env.get(variable);
	}
}
0reactions
paulschwarzcommented, Mar 26, 2020

@jessevdp are you using a framework? If not, your Config class might have to do. But if yes, you might want to find a way to fit dotenv into there in a properly integrated fashion. Here is my example using Spring https://github.com/paulschwarz/spring-dotenv

Beyond a simple static helper, I’m not sure that java-dotenv should do too much.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use Node Environment Variables with a DotEnv File ...
Environment variables are supported out of the box with Node and are accessible via the env object (which is a property of the...
Read more >
Master environment variables on Node.js with dotenv-expand
js process starts at runtime, it will automatically provide access to all existing environment variables, by creating an object (env from now on) ......
Read more >
Managing Environment Variables in Node.js with dotenv
With Node.js apps, environment variables are available through the process.env global variable. We can set the environment variables before we ...
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. Creating these ...
Read more >
How to use environment variables with Node.js - dotenv tutorial
Dotenv is a zero-dependency module that loads environment variables from a . env file into process. env. Storing configuration in the ...
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