Two ways to install JDK 7 / JDK8 in Ubuntu

There are two ways to install jdk in ubuntu:
1: Install through ppa (source).

2: Download the installation package from the official website and install it.

The first one is recommended here, because the jdk upgrade can be easily obtained by apt-get upgrade

Install using ppa/source mode
1. Add ppa

sudo add-apt-repository ppa:webupd8team/java

sudo apt-get update
2.安装oracle-java-installer

 jdk7

sudo apt-get install oracle-java7-installer
 jdk8

sudo apt-get install oracle-java8-installer
The installer will prompt you to agree to oracle's terms of service, select ok

Then select yes

If you are lazy and don't want to click manually. You can also add the following command to agree to the terms by default:

JDK7 default selection clause

echo oracle-java7-installer shared/accepted-oracle-license-v1-1 select true | sudo /usr/bin/debconf-set-selections
JDK8 default selection terms

echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | sudo /usr/bin/debconf-set-selections
The next step is to wait (depending on personal network speed)

If you download the installer very slowly due to firewall or other reasons, you can interrupt the operation. Then download the tar.gz package of the corresponding jdk and put it in:

/var/cache/oracle-jdk7-installer (jdk7)

/var/cache/oracle-jdk8-installer (jdk8)

Below, and then install the installer once. The installer will use the tar.gz package you downloaded by default

3. Set the system default jdk

JDk7

sudo update-java-alternatives -s java-7-oracle
JDK8

sudo update-java-alternatives -s java-8-oracle
If both jdk7 and jdk8 are installed, to switch between the two, you can:

  jdk8 switch to jdk7

sudo update-java-alternatives -s java-7-oracle
  jdk7 切换到jdk8

sudo update-java-alternatives -s java-8-oracle
4. Test if jdk is installed successfully:

java -version

javac -version
directly downloads the jdk compressed package for installation (only jdk7 is introduced here, the principle of jdk8 is exactly the same)
 is divided into the following 5 steps

1. Download JDK from the official website

2. Unzip it and put it in the specified directory

3. Configure environment variables

4. Set the system default JDK

 5. Test jdk

1. Download JDK from the official website   

 地址: http://www.oracle.com/technetwork/articles/javase/index-jsp-138363.html

  Select the corresponding .gz package to download

  1. Unzip it and put it in the specified directory (take jdk-7u60-linux-x64.gz as an example)

      Create a directory:

sudo mkdir /usr/lib/jvm
 and zip to this directory:

sudo tar -zxvf jdk-7u60-linux-x64.gz -C /usr/lib/jvm
3. Modify environment variables:  


 Append the following to the end of the sudo vim ~/.bashrc file:

set oracle jdk environment

export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_60 ## Note here that the directory should be replaced with the jdk directory you decompressed yourself
export JRE_HOME= J A V A H O M E / j r e e x p O r t C L A S S P A T H = . : {JAVA_HOME}/lib: J R E H O M E / l i b e x p O r t P A T H = {JAVA_HOME}/bin:$PATH
 makes environment variables take effect immediately

source ~/.bashrc
4. Set the system default jdk version

sudo update-alternatives –install /usr/bin/java java /usr/lib/jvm/jdk1.7.0_60/bin/java 300
sudo update-alternatives –install /usr/bin/javac javac /usr/lib/jvm/jdk1.7.0_60/bin/javac 300
sudo update-alternatives –install /usr/bin/jar jar /usr/lib/jvm/jdk1.7.0_60/bin/jar 300
sudo update-alternatives –install /usr/bin/javah javah /usr/lib/jvm/jdk1.7.0_60/bin/javah 300
sudo update-alternatives –install /usr/bin/javap javap /usr/lib/jvm/jdk1.7.0_60/bin/javap 300
 然后执行:

sudo update-alternatives –config java
If jdk is installed for the first time, there will be the following prompt

There is only one alternative in link group java (providing /usr/bin/java):
/usr/lib/jvm/jdk1.7.0_60/bin/java

 Otherwise, choose the appropriate jdk

5. Test jdk

java -version

java version “1.7.0_60”
Java(TM) SE Runtime Environment (build 1.7.0_60-b19)
Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)

jdk installed successfully

The two ways to install jdk7 and jdk8 in ubuntu have been introduced

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326003392&siteId=291194637