1. Download
First, of course, download it.
Address: www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Choose a different version as needed.
2. Decompression
Unzip the downloaded .tar.gz file.
Use the following command to decompress:
Copy the code code as follows:
sudo tar zxvf ./jdk-7u45-linux-x64.tar.gz
For easier management, the unzipped files can be moved to another folder.
It’s okay not to do it.
The author moved the file to the /opt/Java/jdk/ directory.
In order to facilitate the setting of environment variables in the next step, the folder was changed to a shorter name - jdk1.7
You can use the following command to rename the folder
Copy the code code as follows:
sudo mv jdk1.7.0_45/ jdk1.7/
3. Set environment variables
Edit the .bashrc file.
Enter the following command in the terminal:
Copy the code code as follows:
vi ~/.bashrc
At the end of the file, add the above lines of code:
Copy the code code as follows:
export JAVA_HOME=/opt/Java/jdk/jdk1.7
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
In order for the changes to take effect immediately, please execute the following command in the terminal:
Copy the code code as follows:
source ~/.bashrc
PS: When there is an error in modifying the PATH of ~/.bashrc and other commands of the system cannot be used, just enter the following in the terminal: and it can be used
Copy the code code as follows:
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games
PS It is best to back up the .bashrc file before modifying it. Backup command:
cp .bashrc .bash_origina