user1580651
user1580651

Reputation: 21

Java System.getEnv()

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?

Upvotes: 2

Views: 8314

Answers (4)

Zero
Zero

Reputation: 2910

Try to write

"$System.env.STOREPWD"

Upvotes: 0

tutak
tutak

Reputation: 1098

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

Upvotes: 1

Acapulco
Acapulco

Reputation: 3533

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.

Upvotes: 1

pb2q
pb2q

Reputation: 59627

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

Upvotes: 0

Related Questions