Ubuntu installation configuration jdk

Keywords: sudo Java Oracle JDK

Install with paa source

1. Install paa

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update

2. Install 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 and select ok
Then select yes

If you are lazy and don't want to click it manually, you can also add the following command, which is the default agreement clause:
JDK7 default selection terms

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

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, you can switch between them:
Switch from jdk8 to jdk7:

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

jdk7 switch to jdk8

sudo update-java-alternatives -s java-8-oracle

4. Test whether the jdk is installed successfully:

java -version
javac -version

Direct download of jdk compressed package installation

1. Download JDK on the official website

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

2. Decompress and put it in the specified directory

sudo tar -zxvf jdk-8u144-linux-x64.tar.gz

3. Modify environment variables:

sudo gedit ~/.bashrc
export JAVA_HOME= #Directory of your own JDK decompression package  
export JRE_HOME=${JAVA_HOME}/jre  
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib  
export PATH=${JAVA_HOME}/bin:$PATH  

Make environment variables effective immediately:

source ~/.bashrc

4. Set the default jdk version of the system

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   

Then execute:

sudo update-alternatives --config java

Last test for successful installation

java -version

Posted by Fixxer on Sat, 30 May 2020 22:05:00 -0700