Java System.getEnv()

♀尐吖头ヾ 提交于 2019-12-11 03:51:32

问题


In mac OSX and in Linux CentOS, I insert a new system environment variable (i.e. "MYAPP") using .bashrc & .bash_profile. I even restarted my laptop (mac) and my server (linux).

When I use the command line "env", that environment variable showed with the correct value. But somehow every time I try to get it in a Java app (desktop app or web app or EJB or servlet any other java app) in either mac or linux, that environment variable ("MYAPP") is not retrieved.

I tried to iterate through the entire environment variables that Java can retrieve and it turns out that it retrieves every environment variables other than "MYAPP". This is very odd.

Anyone know how to solve this?


回答1:


Did you export MYAPP=...? Exporting the variable makes it available to child processes, like java being run by your shell.




回答2:


In Linux, if you only set the variable (or export it) in a bash session, it will be available to a kind of "sub" session, which is only available to the command you just executed, and nothing else.

You could probably use the dot operator in bash (also called "source" command). From the page:

When a script is run using `source' it runs within the existing shell, any variables created or modified by the script will remain available after the script completes.

So you could try doing . export VARIABLE=value, and then running your java program. This is similar to setting a variable in a Windows terminal, and then opening a new terminal and expecting the env var to be there. It won't.

This way, you are telling bash "this command should be available in this specific session (the session's process)". OTherwise you are telling it "set this env var for the bash session that will end after I run this export command" thus, it won't exist when you run your Java program.




回答3:


After having defined and exported the environment variable. Launch your IDE from the same Terminal.




回答4:


Try to write

"$System.env.STOREPWD"



来源:https://stackoverflow.com/questions/11838219/java-system-getenv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!