9/18/2014

Switch JDK verson on Mac

When you installed many of jdk versions, there are two ways to switch the jdk.

  • Using Java Preferences application, but Apple decided to remove it.
  • Using java_home in console

After a lot of googing a found simple way solution, I’d like to share.

Edit your profile /.bash_profile or /.zshrc

alias java_ls='/usr/libexec/java_home -V 2>&1 | grep -E "\d.\d.\d[,_]" | cut -d , -f 1 | colrm 1 4 | grep -v Home'

function java_use() {
export JAVA_HOME=$(/usr/libexec/java_home -v $1)
export PATH=$JAVA_HOME/bin:$PATH
java -version
}

Execute java_ls

1.8.0_11
1.7.0_45
1.6.0_65-b14-462
1.6.0_65-b14-462

Execute java_use 1.8

java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)

1 comment: