在.bashrc文件的末尾插入环境变量:
$ 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
当我运行代码段时:
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
System.out.format("%s=%s%n", envName, env.get(envName));
}
显示我的所有环境变量:
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...
为什么Java代码不显示环境变量$JAVA_HOME
和$M2_HOME
?
答案 0 :(得分:3)
我认为您需要使用export命令将 shell变量导出为环境变量。
export JAVA_HOME="/usr/lib/jvm/java-8-oracle/"
export CLASSPATH="JAVA_HOME/lib/:$CLASSPATH"
export PATH="$JAVA_HOME/bin/:$PATH"
然后一定要reload your .bashrc