Archive
Running Java Apps with a specific version of JRE or JDK on Arch Linux
Sometimes when we use,test or develop some Java applications there might be a requirement to use a specific version of JRE or JDK. By different versions I mean Java 7 (JRE 7 or JDK 7) or Java 8 (JRE 8 or JDK 8). Sometimes depending on the server setup even a specific minor version. For example when our application should run on a server on which we don’t have admin privileges to install different version of Java not and because of some policies the server admins don’t allowed another version to be installed.
On my machine (GNU/Linux Arch distribution) I’m usually running the latest version of the JDK (at the moment of writing this post JDK 1.8.0_60 (build 1.8.0_60-b27).
Till now, I had a small script “/etc/profile.d/java-env.sh” which was defining the JAVA_HOME parameter and when I need to use a different version of the JDK i would just open the terminal edit the file, reload the profile and start the Java app.
The contents of “/etc/profile.d/java-env.sh” were:
#$ export JAVA_HOME=<PATH_TO_THE_JAVA_7_INSTALLATION> $ export JAVA_HOME=<PATH_TO_THE_JAVA_8_INSTALLATION> $ export PATH=$PATH:$JAVA_HOME/bin
I was doing this for a while… I was a bit lazy to find a better solution, but I wanted to save myself from doing this over and over again. Recently I did a fresh install of my GNU/Linux so I thought it was time to fix this problem too.
When I was searching for a solution for this problem I came across this answer on Stack Exchange. The answer was good enough, but I didn’t want to write the whole path to the Java installation so I edited it this way:
#!/bin/bash
case "$1" in
7)
JAVA_HOME=<PATH_TO_THE_JAVA_7_INSTALLATION>
;;
8)
JAVA_HOME=<PATH_TO_THE_JAVA_8_INSTALLATION>
;;
*)
JAVA_HOME=$1
esac
export JAVA_HOME; shift
PATH=${JAVA_HOME}/bin:$PATH
$*
I named this script “javawrapper.sh”. To test if it works try executing the following commands in the terminal
$ ./javawrapper.sh 7 <APPLICATION_TO_START> $ ./javawrapper.sh 7 java -version $ ./javawrapper.sh 8 java -version
You will see the version of your Java 7 or 8 🙂
Someone my ask why does this work because after the second JAVA_HOME we have two java versions defined in our PATH. It works because we are adding the new, temporal JAVA_HOME at beginning of the PATH variable and when the GNU/Linux operating system processes the PATH variable value the first java executable that will be found and executed is in our newly added JAVA_HOME path.
After adding this script my “/etc/profile.d/java-env.sh” looks like this:
$ export JAVA_HOME=<PATH_TO_THE_JAVA_8_INSTALLATION> $ export PATH=$PATH:$JAVA_HOME/bin
I hope this helps and I would like to hear how did the other solve this problem. Just one more quick note for the Debian users who will probably suggest using “update-alternatives”: there is no such thing in Arch Linix.
Happy hacking 🙂