2

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?

2
  • Please post a code snippet of what you have tried. Commented Aug 7, 2012 at 1:42
  • The obvious answer would be that whatever you are using to launch your Java apps is not using the environment settings associated with the account whose bashrc / bash_profile you added the environment variable to. Maybe you added them to the wrong account? Commented Aug 7, 2012 at 2:02

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

0

Try to write

"$System.env.STOREPWD"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.