You can set your JAVA_HOME in /etc/profile
But the preferred location for JAVA_HOME or any system variable is /etc/environment.
Open /etc/environment in any text editor like nano or gedit and add the following line:
JAVA_HOME="/usr/lib/jvm/open-jdk"
(java path could be different)
Use source to load the variables, by running this command:
source /etc/environment
Then check the variable, by running this command:
echo $JAVA_HOME
Usually most linux systems source /etc/environment by default. If your system doesn't do that add the following line to ~/.bashrc (Thanks @pje)
source /etc/environment
/etc/environment 设置的也是全局变量,从文件本身的作用上来说, /etc/environment 设置的是整个系统的环境,
而/etc/profile是设置所有用户的环境。
有几点需注意:
系统先读取 etc/profile 再读取 /etc/environment(还是反过来?) /etc/environment 中不能包含命令,即直接通过 VAR="..." 的方式设置,不使用 export 。 使用 source /etc/environment 可以使变量设置在当前窗口立即生效,需注销/重启之后,才能对每个新终端窗口都生效。
只对当前shell/bash 有效