What is the proper (2021 way) of creating a permanent environment variable on a Mac (macOS Big Sur) and then use it within a Java project.
There are many very old posts regarding this topic. None of them seem to work properly nowadays.
- How to add a permanent environment value (through terminal)?
- And how can I use it in a Java code?
I'm also not sure how I was able to add my testvar=testvalue
to the list, because I tried so many files (although it seems none of them worked), by adding export testvar=testvalue
to the following files:
- /etc/paths
- ~/.bashrc
- ~/.bash_profile
- ~/.profile
- /etc/profile
Also after inserting it into each file I used source {file}
.
So at this point I have no idea which is the proper way to create and have it permanently, and being able to use it in my Java code.
So far, I can print the variables into the terminal like this:
printenv
My variables are getting listed, example:
testvar=testvalue
In my Java code, I get null when using:
System.getenv("testvar")
However using an other variable names that were not created by me, but the macOS system (eg. "USER") prints the value as expected.