Discussion on three ways to configure linux environment variables of take java as an example

  • 2020-05-15 03:20:10
  • OfStack

1. Modify /etc/profile files

This approach is recommended if your computer is used only for development purposes, as all users of shell have access to these environment variables, which can cause security problems for your system.

· open /etc/profile with a text editor
· add at the end of the profile file:


export JAVA_HOME=/usr/share/jdk1.6.0_14
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

· login again

2. Modify.bash_profile file

This is a more secure way of controlling access to these environment variables to the user level. If you want to give a user access to these environment variables, you can simply modify the.bash_profile file in the user's home directory.

· open the.bash_profile file in the user directory with a text editor
· add at the end of the.bash_profile file:

export JAVA_HOME=/usr/share/jdk1.6.0_14
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

· login again

3. Directly set variables under shell

I don't agree with this method, because if you change shell, your Settings will be invalid. Therefore, this method is only used temporarily, and you will have to reset it when you want to use it later, which is troublesome.

Simply execute the following command at the shell terminal:
export JAVA_HOME=/usr/share/jdk1.6.0_14
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar


Related articles: