In this post we are going to discuss “How to make two different java homes( two different java version) for the two users in linux ? “. This is mainly needed when we run two different application which needs two different java version.
Say for example, there is a Banking Application which needs java5 and there is another application called Household Management which needs java6. The Banking Application used by many users in the server and only few members use Household Application. To run those application we need respective java version should be available on the server. This can be achieved by modifying the profile values of the respective users.
Lets see how this can be done.
There is a system wide global environment file ‘/etc/environment’. In this file we can see PATH is set.
when we type anything on command line, it will search the directory which is set for PATH as mentioned in snapshot1.
Lets verify this,
Just type ‘date’ in command line. It will search the date command by the order which is specified for PATH in global environment file. It will search the ‘date’ command first on the path ‘/usr/local/sbin’ if it find it will return from this directory and if not it goes to next path ‘/usr/local/bin’ then on ‘/usr/sbin’ and then on ‘/usr/bin’ and so on, where as ‘:’ is path separator.
Since date command is available in ‘/bin’ it will take from this directory. Suppose we have same date binaries with higher version which is in ‘/usr/games’, it will not be picked from here because its already been found in ‘/bin’ directory.
Snapshot : 2
Explanatory Note :
- Green Encircled : cat /etc/envir* file
- Orange Encircled : We have created simple C file that prints “Techlister”
- We compiled the C file and we renamed ‘a.out’ file to ‘date’
- Now We moved the ‘date’ executable(which is created in step3) file to /usr/local/sbin which is prior to /bin directory
- Yellow Encircled : Exit and Re-login to user and check the date by issuing the command ‘date’. we can notice that, it executes our date and it prints “TECHLISTER”
Our Observation :
- PATH gives you the Prioritized Directory Structure.
- “FIRST FOUND FIRST DISPLAYED” no matter what the best version which is available on later directory.
In our case, Many Users want java5 so add the java home in /etc/environment file. Since .bash_profile use $PATH value from /etc/environment file. All users take java5 as default.
.bash_profile File :
# .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin export PATH
In the line number 10 we can notice that, $PATH is from system wide global environment file /etc/environment.
Those who need java6 we are going to change their .bash_profile file as below
PATH=/home/sathish/java:$PATH:$HOME/bin export PATH
Here we can notice that before loading $PATH, we are appending java6 version path to the front of $PATH. Logout and re-login to the respective user to get the changes. Now verify java version.
That’s it we done it.
- System Wide Global Environment File is /etc/environment. In this File you can note that PATH is set.
- This PATH will be called in .bash_profile file at the time of loggin.
- You can override the directory priorities of PATH by appending Front side as explained in above cases.
- Any thing which you type on command line it will search based PATH values, this default manner can be override by user specific profile values.
- If nothing find on any directory which is set by .bash_profile, then only it displays ‘command not found’.
- Suppose some user need two java version, that can be be done by two profile files. But default it takes .bash_profile we can have other profile file that can be run on demand(its purely by terminal session). So on one terminal session you can run java5 and other on java6.
Go Mad With Linux, She will never Hurt you 🙂