Multiple version of java in Linux with alternatives

  1. Download the required jdk

# cd /usr/java/
# wget –no-cookies –no-check-certificate –header “Cookie:; oraclelicense=accept-securebackup-cookie” “”

# tar xzf jdk-7u79-linux-x64.tar.gz

2. Install java with Alternatives

# cd /usr/java
# alternatives –install /usr/bin/java java /usr/java/jdk1.7.0_79/bin/java 2
# alternatives –config java
There are 3 programs which provide ‘java’.

Selection Command
* 1 /opt/jdk1.7.0_60/bin/java
+ 2 /opt/jdk1.7.0_72/bin/java
3 /usr/java/jdk1.7.0_79/bin/java

Enter to keep the current selection[+], or type selection number: 3 [Press Enter]


3. Now you may also required to set up javac and jar commands path using alternatives command.

# alternatives –install /usr/bin/jar jar /usr/java/jdk1.7.0_79/bin/jar 2
# alternatives –install /usr/bin/javac javac /usr/java/jdk1.7.0_79/bin/javac 2
# alternatives –set jar /usr/java/jdk1.7.0_79/bin/jar
# alternatives –set javac /usr/java/jdk1.7.0_79/bin/javac

4. Check java version

# java -version

java version “1.7.0_79”
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

5. Configuring Environment Variables

Most of Java based application’s uses environment variables to work. Use following commands to set up these variable properly. It’s also good to add following commands to any start-up script like ~/.bashrc or ~/.bash_profile.

Setup JAVA_HOME Variable
# export JAVA_HOME=/usr/java/jdk1.7.0_79
Setup JRE_HOME Variable
# export JRE_HOME=/usr/java/jdk1.7.0_79/jre
Setup PATH Variable
# export PATH=$PATH:/usr/java/jdk1.7.0_79/bin:/opt/jdk1.7.0_79/jre/bin

Hope this helps. With the above steps you can install multiple version of Java at the same time but you can use only one version at a time.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s