Install different java versions, using them simultaneously in Linux

This may seem trivial, but I have seen many people do not know how to take a single machine, several Java application servers running on different versions of Java

To start for safety as recommended, it is that for every application server we have running, the facility owner is a different user, the same user is running the application server.

In short, for each user you can configure your runtime environment by default, so you do not need to touch anything in the application servers to indicate that Java should use.

Take the example that we want to run Tomcat 8 with Java 8 and Tomcat 6 with Java 6.

 

The first thing we will do is download and unzip versions of Java 8 (http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) and 1.5 (from archived versions http://www.oracle.com/technetwork/java/javase/archive-139210.html).

I prefer to download the * .tgz or older versions .bin (nothing rpm) to be perfectly controlled where to install the software.

Let’s unpack the Java in /opt:

Source   
[root@nostromo opt]# pwd
/opt
[root@nostromo opt]# ls -lrt
total 199332
-rw-r--r-- 1 root root  44191305 abr 17 12:45 jdk-1_5_0_22-linux-amd64.bin
-rw-r--r-- 1 root root 159910666 abr 17 12:49 jdk-8u5-linux-i586.tar.gz
 
[root@nostromo opt]# tar xfz jdk-8u5-linux-i586.tar.gz
...
 
[root@nostromo opt]# chmod +x jdk-1_5_0_22-linux-amd64.bin
...
[root@nostromo opt]# ls -l
total 199336
drwxr-xr-x 9 root root      4096 oct  9  2009 jdk1.5.0_22
-rwxr-xr-x 1 root root  44191305 abr 17 12:45 jdk-1_5_0_22-linux-amd64.bin
drwxr-xr-x 8   10  143      4096 mar 18 09:03 jdk1.8.0_05
-rw-r--r-- 1 root root 159910666 abr 17 12:49 jdk-8u5-linux-i586.tar.gz

We create operating system users (called simplest possible):

Source   
[root@nostromo ~]# adduser tomcat8
[root@nostromo ~]# adduser tomcat6

Download Tomcat 8 (from http://tomcat.apache.org/download-80.cgi) and Tomcat 6 (from http://tomcat.apache.org/download-60.cgi), we leave them in /opt. We unzip and give ownership to users created:

Source   
[root@nostromo opt]# ls -l
total 215020
-rw-r--r-- 1 root root   7065481 abr 17 12:56 apache-tomcat-6.0.39.tar.gz
-rw-r--r-- 1 root root   8993812 abr 17 12:56 apache-tomcat-8.0.5.tar.gz
drwxr-xr-x 9 root root      4096 oct  9  2009 jdk1.5.0_22
-rwxr-xr-x 1 root root  44191305 abr 17 12:45 jdk-1_5_0_22-linux-amd64.bin
drwxr-xr-x 8   10  143      4096 mar 18 09:03 jdk1.8.0_05
-rw-r--r-- 1 root root 159910666 abr 17 12:49 jdk-8u5-linux-i586.tar.gz
[root@nostromo opt]# tar xfz apache-tomcat-6.0.39.tar.gz
[root@nostromo opt]# tar xfz apache-tomcat-8.0.5.tar.gz
[root@nostromo opt]# ls -l
total 215028
drwxr-xr-x 9 root root      4096 abr 17 12:59 apache-tomcat-6.0.39
-rw-r--r-- 1 root root   7065481 abr 17 12:56 apache-tomcat-6.0.39.tar.gz
drwxr-xr-x 9 root root      4096 abr 17 12:59 apache-tomcat-8.0.5
-rw-r--r-- 1 root root   8993812 abr 17 12:56 apache-tomcat-8.0.5.tar.gz
drwxr-xr-x 9 root root      4096 oct  9  2009 jdk1.5.0_22
-rwxr-xr-x 1 root root  44191305 abr 17 12:45 jdk-1_5_0_22-linux-amd64.bin
drwxr-xr-x 8   10  143      4096 mar 18 09:03 jdk1.8.0_05
-rw-r--r-- 1 root root 159910666 abr 17 12:49 jdk-8u5-linux-i586.tar.gz
[root@nostromo opt]# tar xfz apache-tomcat-6.0.39.tar.gz
[root@nostromo opt]# tar xfz apache-tomcat-8.0.5.tar.gz
[root@nostromo opt]# ls -l
total 215028
drwxr-xr-x 9 tomcat6 tomcat6      4096 abr 17 12:59 apache-tomcat-6.0.39
-rw-r--r-- 1 root    root      7065481 abr 17 12:56 apache-tomcat-6.0.39.tar.gz
drwxr-xr-x 9 tomcat8 tomcat8      4096 abr 17 12:59 apache-tomcat-8.0.5
-rw-r--r-- 1 root    root      8993812 abr 17 12:56 apache-tomcat-8.0.5.tar.gz
drwxr-xr-x 9 root    root         4096 oct  9  2009 jdk1.5.0_22
-rwxr-xr-x 1 root    root     44191305 abr 17 12:45 jdk-1_5_0_22-linux-amd64.bin
drwxr-xr-x 8      10     143      4096 mar 18 09:03 jdk1.8.0_05
-rw-r--r-- 1 root    root    159910666 abr 17 12:49 jdk-8u5-linux-i586.tar.gz

In this situation we have created users, java and tomcat installed in addition to modified the permissions.

Let’s set user tomcat6, so take the default Java 1.5:

Source   
[root@nostromo opt]# su - tomcat6
[tomcat6@nostromo ~]$ ls /opt/jdk1.5.0_22/
bin  COPYRIGHT  demo  include  jre  lib  LICENSE  man  README.html  sample  src.zip  THIRDPARTYLICENSEREADME.txt
[tomcat6@nostromo ~]$ cat .bash_profile
# .bash_profile
 
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
 
# User specific environment and startup programs
 
PATH=$PATH:$HOME/.local/bin:$HOME/bin
 
export PATH

Edit the .bash_profile (RedHat/CentOS, Ubuntu find in a .profile) and modified to make it something like:

Source   
# .bash_profile
 
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
 
# User specific environment and startup programs
 
export JAVA_HOME=/opt/jdk1.5.0_22
PATH=$JAVA_HOME/bi:$PATH:$HOME/.local/bin:$HOME/bin
 
export PATH

What has been done is set the JAVA_HOME variable to point to the Java installation to be used, this variable is advisable to have it defined for many application servers, greatly facilitates the configuration. Also it added in the PATH the bin directory of the Java installation.

After editing the .bash_profile, we left the session and re-enter. We can verify that the version used by the user is required and also run Tomcat 6 smoothly with the right Java, then we stopped so that we can verify the installation of Tomcat 8 and there is no a port conflict:

Source   
[tomcat6@nostromo ~]$ vi .bash_profile
[tomcat6@nostromo ~]$ exit
logout
[root@nostromo opt]# su - tomcat6
Último inicio de sesión:jue abr 17 13:04:26 CEST 2014en pts/1
[tomcat6@nostromo ~]$ java -version
java version "1.5.0_22"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_22-b03)
Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_22-b03, mixed mode)
[tomcat6@nostromo ~]$ /opt/apache-tomcat-6.0.39/bin/startup.sh
Using CATALINA_BASE:   /opt/apache-tomcat-6.0.39
Using CATALINA_HOME:   /opt/apache-tomcat-6.0.39
Using CATALINA_TMPDIR: /opt/apache-tomcat-6.0.39/temp
Using JRE_HOME:        /opt/jdk1.5.0_22
Using CLASSPATH:       /opt/apache-tomcat-6.0.39/bin/bootstrap.jar
[tomcat6@nostromo ~]$ ps -fe | grep java
tomcat6  16658     1 68 13:12 pts/1    00:00:06 /opt/jdk1.5.0_22/bin/java -Djava.util.logging.config.file=/opt/apache-tomcat-6.0.39/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/opt/apache-tomcat-6.0.39/endorsed -classpath /opt/apache-tomcat-6.0.39/bin/bootstrap.jar -Dcatalina.base=/opt/apache-tomcat-6.0.39 -Dcatalina.home=/opt/apache-tomcat-6.0.39 -Djava.io.tmpdir=/opt/apache-tomcat-6.0.39/temp org.apache.catalina.startup.Bootstrap start
tomcat6  16681 16593  0 13:12 pts/1    00:00:00 grep --color=auto java
[tomcat6@nostromo ~]$ /opt/apache-tomcat-6.0.39/bin/shutdown.sh
Using CATALINA_BASE:   /opt/apache-tomcat-6.0.39
Using CATALINA_HOME:   /opt/apache-tomcat-6.0.39
Using CATALINA_TMPDIR: /opt/apache-tomcat-6.0.39/temp
Using JRE_HOME:        /opt/jdk1.5.0_22
Using CLASSPATH:       /opt/apache-tomcat-6.0.39/bin/bootstrap.jar
[tomcat6@nostromo ~]$
[tomcat6@nostromo ~]$ ps -fe | grep java
tomcat6  17029 16933  0 13:20 pts/1    00:00:00 grep --color=auto java

You can see that the owner of the process is appropriate.

User tomcat8, the steps are exactly the same, logically changing the location of Java and calls to start Tomcat:

Source   
[tomcat6@nostromo ~]$ exit
logout
[root@nostromo opt]# su - tomcat8
[tomcat8@nostromo ~]$ vi .bash_profile
[tomcat8@nostromo ~]$ cat .bash_profile
# .bash_profile
 
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
 
# User specific environment and startup programs
export JAVA_HOME=/opt/jdk1.8.0_05
PATH=$JAVA_HOME/bin:$PATH:$HOME/.local/bin:$HOME/bin
 
export PATH
[tomcat8@nostromo ~]$ exit
logout
[root@nostromo opt]# su - tomcat8
Último inicio de sesión:jue abr 17 13:14:45 CEST 2014en pts/1
[tomcat8@nostromo ~]$ java -version
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) Server VM (build 25.5-b02, mixed mode)
[tomcat8@nostromo ~]$ /opt/apache-tomcat-8.0.5/bin/startup.sh
Using CATALINA_BASE:   /opt/apache-tomcat-8.0.5
Using CATALINA_HOME:   /opt/apache-tomcat-8.0.5
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.0.5/temp
Using JRE_HOME:        /opt/jdk1.8.0_05
Using CLASSPATH:       /opt/apache-tomcat-8.0.5/bin/bootstrap.jar:/opt/apache-tomcat-8.0.5/bin/tomcat-juli.jar
Tomcat started.
[tomcat8@nostromo ~]$ ps -fe | grep java
tomcat8  17087     1 99 13:21 pts/1    00:00:04 /opt/jdk1.8.0_05/bin/java -Djava.util.logging.config.file=/opt/apache-tomcat-8.0.5/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/opt/apache-tomcat-8.0.5/endorsed -classpath /opt/apache-tomcat-8.0.5/bin/bootstrap.jar:/opt/apache-tomcat-8.0.5/bin/tomcat-juli.jar -Dcatalina.base=/opt/apache-tomcat-8.0.5 -Dcatalina.home=/opt/apache-tomcat-8.0.5 -Djava.io.tmpdir=/opt/apache-tomcat-8.0.5/temp org.apache.catalina.startup.Bootstrap start
tomcat8  17117 17039  0 13:21 pts/1    00:00:00 grep --color=auto java
[tomcat8@nostromo ~]$ /opt/apache-tomcat-8.0.5/bin/shutdown.sh
Using CATALINA_BASE:   /opt/apache-tomcat-8.0.5
Using CATALINA_HOME:   /opt/apache-tomcat-8.0.5
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.0.5/temp
Using JRE_HOME:        /opt/jdk1.8.0_05
Using CLASSPATH:       /opt/apache-tomcat-8.0.5/bin/bootstrap.jar:/opt/apache-tomcat-8.0.5/bin/tomcat-juli.jar

We have been working with Tomcat as the application server but is applicable to any, JBoss, Weblogic, etc … even application server software on any Java command line.

I hope it helps you…

Leave a Reply