Skip to content

Instantly share code, notes, and snippets.

@peschee
Forked from jvwong/java_switch.md
Last active September 30, 2016 08:38
Show Gist options
  • Save peschee/36dd073bb2185573b93b3e27be82cffc to your computer and use it in GitHub Desktop.
Save peschee/36dd073bb2185573b93b3e27be82cffc to your computer and use it in GitHub Desktop.
Switching Java on OSX

List the current installed Java versions

$ /usr/libexec/java_home -V

Install the version desired

Apple provides their own version of 6 (Java for OS X 2015-001)

List the current installed Java versions

You should see the version installed

Matching Java Virtual Machines (2):
    1.8.0_25, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
    1.7.0_45, x86_64:	"Java SE 7"	/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home

Select the version

$ export JAVA_HOME=`/usr/libexec/java_home -v 1.7`

Confirm the java version

$ export JAVA_HOME=`/usr/libexec/java_home -v 1.7`
$ java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-468, mixed mode)

Installing this as a handy function in ZSH

http://stackoverflow.com/questions/21964709/how-to-set-or-change-the-default-java-jdk-version-on-os-x

# Function that changes the JAVA_HOME variable to the given java version.
function jhome () {
  export JAVA_HOME=$(/usr/libexec/java_home -v "$@")
  echo "JAVA_HOME:" $JAVA_HOME
  echo "java -version:"
  java -version
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment