Skip to content

Instantly share code, notes, and snippets.

@paulwellnerbou
Last active November 8, 2017 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulwellnerbou/8b7907c067047ad553c8c97aefc4365d to your computer and use it in GitHub Desktop.
Save paulwellnerbou/8b7907c067047ad553c8c97aefc4365d to your computer and use it in GitHub Desktop.
Shell script switching between two Java versions, including JAVA_HOME (which is not handled by update-alternatives)
#!/bin/bash
set -u
JAVA7="/opt/jdk1.7.0_80"
JAVA8="/opt/jdk1.8.0_31"
# Assuming you have $JAVA_SYMLINK/bin in your path, in this case "/opt/jdk/bin"
JAVA_SYMLINK="/opt/jdk"
NEW_JAVA=$JAVA7
current=`readlink $JAVA_SYMLINK`
echo $current
if [ "$current" == "$JAVA7" ]; then
NEW_JAVA=$JAVA8
fi
echo "$current -> $NEW_JAVA"
unlink $JAVA_SYMLINK
ln -s $NEW_JAVA $JAVA_SYMLINK
java -version
which java
echo $JAVA_HOME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment