Switching between Java7 and Java8 in Lucene/Solr


Lucene/Solr trunk (the future 6.0 release) is now on Java8, while version 5.x is still on Java7.
Linux and Windows allows one to install a JDK any place in the filesystem, and I use the convention of installing in
/opt/jdk7 and /opt/jdk8. Things are a little more difficult on Mac OS-X however, as you can’t chose the install location. Luckily there is a command called java_home to show you where a JDK is installed.

Here’s a snippet from my .profile to help manage working with different java versions:

OS=`uname`
case "$OS" in
 CYGWIN*)
  OS=cygwin
  OPT=c:/opt
  ;;
 *)
  OPT=/opt 
  ;;
esac


set-java () {
  export JAVA_HOME="$*"
  if [ $OS = "cygwin" ]; then
    export PATH="`cygpath $JAVA_HOME/bin`:$PATH"
  else 
    export PATH="$JAVA_HOME/bin:$PATH"
  fi
}

if [ $OS = "Darwin" ]; then
  JAVA7=`/usr/libexec/java_home -v 1.7`
  JAVA8=`/usr/libexec/java_home -v 1.8`
else 
  JAVA7=$OPT/jdk7
  JAVA8=$OPT/jdk8
fi

set-java $JAVA8

Now, if I switch from working on trunk to working on Lucene 5 or Solr 5, I can easily switch the default JDK for a single terminal via the set-java shell function.

/opt/heliosearch$ java -version
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

/opt/heliosearch$ set-java $JAVA7

/opt/heliosearch$ java -version
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

/opt/heliosearch$