Installing multiple versions of jdk on Linux system

When a friend mentioned this, he tried to make a record for later.

Installation environment: CentOS release 6.5 (Final) 64 bit

1. Install a jdk first, taking jdk 9 as an example

[root@local opt]# ls
jdk-9.0.4_linux-x64_bin.tar.gz
[root@local opt]# tar zxvf jdk-9.0.4_linux-x64_bin.tar.gz 
...
[root@local opt]# ls
jdk-9.0.4  jdk-9.0.4_linux-x64_bin.tar.gz
[root@local opt]# mv jdk-9.0.4 jdk9
[root@local opt]# ls
jdk9  jdk-9.0.4_linux-x64_bin.tar.gz

The middle decompression process is omitted, and the renaming depends on personal preference, which does not affect the final effect.

Back up the profile file and add the jdk environment variable at the end of the file

[root@local opt]# cp /etc/profile /etc/profile.bak
[root@local opt]# vi /etc/profile
...

# jdk
export JAVA_HOME=/opt/jdk9
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

Validate the configuration and view the jdk information

[root@local opt]# source /etc/profile
[root@local opt]# java -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)

2. Install another version of jdk, taking jdk 9 as an example
Switch users and unzip

[root@local opt]# su - admin
[admin@local ~]$ ls
jdk-8u161-linux-x64.tar.gz
[admin@local ~]$ tar -zxvf jdk-8u161-linux-x64.tar.gz 
...
[admin@local ~]$ mv jdk1.8.0_161/ jdk8
[admin@local ~]$ ls
jdk8  jdk-8u161-linux-x64.tar.gz

Change the user profile.bashrc and add the jdk environment variable

[admin@local ~]$ cp .bashrc .bashrc.bak
[admin@local ~]$ vi .bashrc
...
# jdk
export JAVA_HOME=/home/admin/jdk8/bin

Validate the configuration and view the jdk information

[admin@local ~]$ source .bashrc
[admin@local ~]$ $JAVA_HOME/java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

At this point, there are two versions of jdk available on the same machine

Tags: JDK Java Linux CentOS

Posted on Sat, 04 Apr 2020 07:31:09 -0700 by jj33