0

Insert the environment variables at the end of .bashrc file:

$JAVA_HOME

JAVA_HOME="/usr/lib/jvm/java-8-oracle/​"
CLASSPATH="JAVA_HOME/lib/:$CLASSPATH"
PATH="$JAVA_HOME/bin/:$PATH"

$M2_HOME

M2_HOME="/usr/share/maven/"
M2="$M2_HOME/bin"
PATH="$M2:$PATH" PATH=$M2:$PATH

When I run the code snippet:

Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
    System.out.format("%s=%s%n", envName, env.get(envName));
}

Displays all my environment variables:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games" 
XAUTHORITY=/home/ricardoramos/.Xauthority
XMODIFIERS=@im=ibus
XDG_DATA_DIRS=/usr/share/ubuntu:/usr/share/gnome:/usr/local/share/:/usr/share/
GDMSESSION=ubuntu
MANDATORY_PATH=/usr/share/gconf/ubuntu.mandatory.path
TEXTDOMAINDIR=/usr/share/locale/
GTK_IM_MODULE=ibus
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-rS3vXA2fJM
DEFAULTS_PATH=/usr/share/gconf/ubuntu.default.path
XDG_CURRENT_DESKTOP=Unity
SWT_GTK3=0
UPSTART_SESSION=unix:abstract=/com/ubuntu/upstart-session/1000/1996...

Why Java code does not display the environment variables $JAVA_HOME and $M2_HOME?

ricardoramos
  • 861
  • 3
  • 16
  • 33
  • 1. When did you set the environment variables? 2. How are you running your Java code? I ask because if you are running the Java code in a stale command prompt, or in a running instance of Eclipse (for example), you may need to restart in order to pick up the latest environment variables and values. – Bobby StJacques Aug 10 '15 at 20:49
  • As soon as I insert the environment variables at the end of my .bashrc file, I restarted the computer and ran the code with the eclipse, but does not display the environment variables $JAVA_HOME and $M2_HOME. – ricardoramos Aug 10 '15 at 20:55
  • Unless you start eclipse from a terminal / shell, it won't necessarily pick up ".bashrc" variables as part of its environment. And even if eclipse does, it could still invoke the JRE using `execle` / `execvpe` to override any environment variables of the eclipse parent process. I think the "right" answer, in development anyway, is to set up those environment variables as parameters in the run/debug target configurations in eclipse. – Brian McFarland Aug 10 '15 at 23:03
  • 2
    http://stackoverflow.com/questions/7048216/environment-variables-in-eclipse – Brian McFarland Aug 10 '15 at 23:05

1 Answers1

3

I think you need to use the export command to export a shell variable as an environment variable.

export JAVA_HOME="/usr/lib/jvm/java-8-oracle/​"
export CLASSPATH="JAVA_HOME/lib/:$CLASSPATH"
export PATH="$JAVA_HOME/bin/:$PATH"

Then be sure to reload your .bashrc

Community
  • 1
  • 1
Samuel
  • 15,583
  • 5
  • 54
  • 70
  • I put the exports as a prefix of my environment variables in my .bashrc file and restarted the computer, but when I run the same code snippet does not display environment variables $JAVA_HOME and $M2_HOME. – ricardoramos Aug 10 '15 at 21:08