Easily changing java versions in OSX
In OSX, they've frankly done a pretty good job of enabling multiple versions of java at the same time, but just as frankly it's somewhat obscure and inconvenient to manage multiple versions. It's not mind bogglingly difficult but for us oldsters who are lazy, I created a convenient way to switch versions inspired by (though nowhere nearly as awesome as) rvm for ruby.
- Download all of the versions of java you want to use from the appropriate location java 1.6, java 7, or java 8. (you need at least one to start with)
- Add the following lines to ~/.bash_profile
- Either source .bash_profile by typing ". ~/.bash_profile" or simply close your terminal and relaunch
jvm() { export JAVA_HOME=`/usr/libexec/java_home -v "$1"` java -version }
At this point you can change versions of java by typing:
jvm 1.6*or
jvm 1.7*Yes, there's more to it, refer to java_home for more version matching options, and it could be way more awesome, but this should be a quick help for those who just need a simple way to switch when troubleshooting/testing jvm version issues and you want to quickly change JDKs in an automated fashion. Note, this also works with fix pack and minor versions, you just need to refer to the version pattern matching of the '-v' option for java_home to know how to use it. edit - I originally had an alias pointing to a function until a gracious commenter pointedly asked why I did it that way. Not having an answer I eliminated the alias. This shows the strength of my convictions about the "right way" to do things...
Comments
> or simply close your terminal and relaunch
This function doesn't set the PATH, which is all that really needs to be done (and that's presumably what's happening in ~/.bash_profile or ~/.bashrc). Note that closing/re-opening a terminal would undo the local changes in the shell made to the env var JAVA_HOME.
The main point of all of this is to put $JAVA_HOME/bin at the beginning of the PATH (i.e., PATH="$JAVA_HOME/bin:$PATH"). Removing the old/unused jvm's from the PATH being ideal but typically unnecessary.
(nb: I came here following your comment in another post; see my comment here: http://www.jayway.com/2014/01/15/how-to-switch-jdk-version-on-mac-os-x-maverick/#comment-149143 )
System Version: OS X 10.9.5 (13F34)
Kernel Version: Darwin 13.4.0
I created a bash file (after installing osx Yosemite and now can easily switch between JDK 1.7 and 1.8.
am using catalina os version is 10.15.7