9

Here is a line in file ~/.bash_profile

export MESSAGE="Hello World"

I want to access system variable MESSAGE in java.

System.getenv("MESSAGE"); doesn't work.

Patrick Lorio
  • 5,520
  • 11
  • 45
  • 74
  • 2
    before you run java do `echo $MESSAGE` to make sure that the environment variable is defined. – pb2q Sep 06 '12 at 16:46

5 Answers5

6

The .bash_profile file is only sourced for login shells. If your java process is spawned from a shell that is not a login shell (for example a script with a #!/bin/sh at the top) then it will not read it (though it may still inherit MESSAGE from the environment depending on how you run it).

Note also that .bash_profile is also not run for interactive shells that are not "login" shells, so you can't rely on it being run even when you have a shell prompt. People usually use .bashrc, which is sourced for all interactive shells, for that purpose.

If you want a variable to be set in all Bourne shell derivatives regardless of whether they are interactive or not, put it in both .profile and .bashrc.

Andy Ross
  • 11,699
  • 1
  • 34
  • 31
1

I would recommend you to test if your environment variable is indeed "defined" by using echo $MESSAGE as suggested above.
In addition, changing the bash_profile file will not effect your current shell,
you must use "source" in order for it to effect your current shell.
I would also recommend reading this article about differences between bashrc and bash_profile.
Maybe you should define the EXPORT at bashrc?

Yair Zaslavsky
  • 4,091
  • 4
  • 20
  • 27
1

This actually gets even more interesting for users who have a .profile (for the old Bourne shell), that gets read in by .bash_profile automatically (providing compatability). In either case, the environment variables are read in once only at login shell startup, and all subshells inherit those variables for free. The .bashrc is for tty-dependent things and unheritables like functions (the old sh used $ENV, I think, if it were set, for something similar).

Your use of ~/.bash_profile looks fine (although single quotes are more reliable than double quotes, which allow some substitutions). Make sure you've logged out and back in between editing that file and trying your test, or use ". ~/.bash_profile" (no quotes and note the leading dot and space, since the dot is the command here).

The article at http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html covers some good things, like using ". ~/.bashrc" at then end of your ~/.bash_profile (excepth that you should use -r, not -f). The comment about using export in your .bashrc is wrong, you should not do this, for two reasons (1) an pretty insignificant performance penalty, (2) a pretty high chance that some command you execute won't get the environment variable - particularly things spawned from your window manager menus and other places where an actual command prompt didn't appear in any of their parents.

Lastly, make sure the $MESSAGE actually exists in your environment - look at the output of the "env" command - if it isn't there, the Java process won't see it unless it's internally creating it and storing it in its own internal copy of the environment variables.

Another note, if you set env variables in the .profile, use this syntax:

VAR=VALUE ; export VAR

...since the old sh shell doesn't support "export VAR=VALUE". Using set -e before a bunch of these and set +e after can remove the need to use "export" at all, if I remember correctly.

Alex North-Keys
  • 4,200
  • 1
  • 20
  • 22
1

Another place to look at: /etc/environment (this may overrule/replace your .bashrc or .bash_profile in shells opened via your IDE)

user2039709
  • 890
  • 7
  • 17
1

enter image description here

Variables in your bash profile only get used in the terminal session you are in, so IntelliJ won't pick them up.

Instead, go to Edit Configurations in your Run Configurations box, and add your environment variables in a semi-colon separated list in the environment variables box!

Johnny Alpha
  • 758
  • 1
  • 8
  • 35