Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

java - How exactly to set up and use environment variables on mac

What is the proper (2021 way) of creating a permanent environment variable on a mac (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.

  1. How to add a permanent environment value (through terminal)
  2. 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 with

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 name -that was not created by me, but the macos system- (eg. "USER" ) prints the value as expected.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...