Apple decided to remove the Java Preferences application in a software update and afterwards in has been difficult to change the JDK version on Mac OS X. I especially noticed this after I installed the JDK 8 early access preview which makes some of the tips that you find on various blogs online useless. After a lot of googling I finally found a simple solution that I’d like to share.
What you do is to edit your ~/.bash_profile
and add the following:
function setjdk() {
if [ $# -ne 0 ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath $JAVA_HOME
fi
export JAVA_HOME=`/usr/libexec/java_home -v $@`
export PATH=$JAVA_HOME/bin:$PATH
fi
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
setjdk 1.7
What the script does is to first remove other JDK versions in the PATH
so that they won’t interfere with our new JDK version. Then it makes some clever use of /usr/libexec/java_home
which is a command that lists installed JDK versions. The -v
argument tells java_home
to return the path of the JDK with the supplied version, for example 1.7. We also update the PATH
to point to the bin
directory of the newly found JAVA_HOME
directory. At the end we can simply execute the function using
setjdk 1.7
which selects the latest installed JDK version of the 1.7 branch. To select a specific version you can simply execute
setjdk 1.7.0_51
instead. Run /usr/libexec/java_home -h
to get more details on how to choose versions.
That’s it! All credits should go to Neeme Praks and his answer on superuser.com.
Pingback: itemprop="name">JDK setting on Mac OS X Maverick causing you pain? | Java Ninja Chronicles By Norris Shelton, Jr
Thanx for the script. Worked well for me.
Thanks man!
I was running something like export JAVA_HOME=`java_home -v 1.6`, but the scripts also takes care of the PATH so it’s definitively more convenient.
For those which, like in my case, don’t have ~/.bash_profile, you can also add it like an script under /usr/local/bin
Many thanks!
Works really great for Java up to version 1.8
That was really helpful.
Thanks!
Thank you very much for yet another very elegant solution to an annoying problem!
Works well but borks on the removeFromPath function when your Path contains a folder that contains spaces, for example “/Applications/Flash Player Debugger.app”.
Thank you so much!
Great. Works perfectly well and solve a really annoying issue :)
Nice one.. thanks!
Thank you thank you, this saved my butt! I can’t believe Apple removed feature to change it trivially though :(
I’m getting this message in terminal after adding you code to .bash_profile
Unable to find any JVMs matching version “1.7”.
And you’re sure that you have Java 7 installed?
This works for applications, but not for applets. I need my Firefox to use Java 1.7 plugin, but after running your script FIrefox still has Java 1.8 plugin and so far I don’t know how to make it switch to Java 1.7.
Thanks for the script anyway!
Pingback: itemprop="name">Why Java Applets should be banned | Yakov Fain's Blog
Thanks for this! Most helpful in managing multiple JDK versions.
This should be added to .bash_profile as part of JDK installation.
Or perhaps burnt into an EPROM on every new Mac sold.
Anyways, thank you for making my day suck a lot less !
Life saver!
Super simple and straight forward! Thank you so much!
Great! Well-explained and just what I was looking for.
Time saver ! Thanks for sharing this.
Thank you Johan, the script works perfectly !!
I refined the script just a little bit to be able to choose from the existing versions, which saves entering the java_home -V:
function setjdk() {
if [ $# -ne 0 ]; then
switchjdk $@
else
echo “Choose from the following Java versions:”
choosejdk
echo “Please enter the correct full version number:”
read jdkPick
switchjdk $jdkPick
fi
}
function switchjdk() {
removeFromPath ‘/System/Library/Frameworks/JavaVM.framework/Home/bin’
if [ -n “${JAVA_HOME+x}” ]; then
removeFromPath $JAVA_HOME
fi
export JAVA_HOME=`/usr/libexec/java_home -v $@`
export PATH=$JAVA_HOME/bin:$PATH
}
function choosejdk() {
allVersions=$(/usr/libexec/java_home -V)
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e “s;:$1;;” -e “s;$1:?;;”)
}
Thanks for this. Really helpful
Works – nice, thanks.
SO awesome! Thanks for reposting this.
This will work as well, and is relatively robust assuming ‘tr’ and ‘sed’ behave nicely (uses bash):
I think JEnv (http://www.jenv.be/) solves the problem nicely, with added bonus of customizing java version for each directory.
Thanks for the tip, seems very useful.
I took a slightly simpler approach with roughly the same effect: http://mikemainguy.blogspot.com/2014/11/easily-changing-java-versions-in-osx.html
Thanks! Works like a charm.
This was AWESOME. Thanks!
Thanks for this function, really useful !
However, I have a question : I need 2 versions (jdk 1.8 for maven and 1.7 for jboss).
To switch between my 2 servers, I have to add or remove your function. Is there a solution to do have something who recognize automatically which version I need ? To avoid editing each time the bash profile ?
Thanks in advance !
Thank you tons!!!
Pingback: itemprop="name">Apache Maven & Homebrew - GeekApproach
I think that line 5 should be: “removeFromPath $JAVA_HOME/bin”; otherwise, it leaves behind a stray “/bin:” each time you use setjdk. Except for that, it works great! I only noticed it because I changed the PATH after the call to setjdk and that borked the directory that I’d added.
Pingback: itemprop="name">Java 8 on OSX 10.9 - Mac-Forums Discussions for Apple Products & Services
THANKS – great help!!!
Thanks. I’m using it with latest version of 1.8.
Great this script is working for me.
Very nice. Thanks.
Thank you very much! Nevertheless, i.m.h.o. this approach is much easier and more flexible:
https://wimdeblauwe.wordpress.com/2014/03/20/switching-easily-between-java-jdks-on-mac-os-x/
Or is there a problem I don’t recognize? Thanks again.
Awesome, awesome, awesome and awesome. Thanks for a great script!
Pingback: itemprop="name">1 – Switch JDK version on OS X | blog.offeryour.com
very nice
Just want to thank you for this very useful tip !
Works great when switching between 7u60 and 8u40
Rocking!!!
Thank you buddy.
I noticed that the removeFromPath was leaving the trailing /bin in the PATH. I corrected it (and made it pass shellcheck linter).
`
#!/bin/bash
#From http://blog.jayway.com/2014/01/15/how-to-switch-jdk-version-on-mac-os-x-maverick/
function setjdk() {
if [ $# -ne 0 ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
#Bash expansion will be "x" if JAVA_HOME set, emptystring if unset
#http://wiki.bash-hackers.org/syntax/pe#use_an_alternate_value
if [ -n "${JAVA_HOME+x}" ]; then
removeJavaFromPath "${JAVA_HOME}/bin"
fi
export JAVA_HOME=$(/usr/libexec/java_home -v "$@")
export PATH=$JAVA_HOME/bin:$PATH
fi
}
function removeJavaFromPath() {
export PATH="$(echo "$PATH" | sed -E -e "s;:$1;;" -e "s;$1:?;;")"
}
`
Ok, the wordpress engine mangled the bash script where I corrected the original’s path bug, and I made it a little more user friendly.
Here’s a clean version:
https://github.com/StevenACoffman/dotfiles/blob/master/bin/shell/functions/java.sh
Beautiful.
There is a different answer in the SuperUser question you linked with more upvotes. I ended up going for that solution – perhaps consider updating your article if you also like that solution better as your article was the top hit on google for me :)
That was superb!!! thanks a lot for solving an annoying problem
I’m getting this error when I ran the file:
Babatundes-iMac:~ tfalade$ source ~/.bash_profile
-bash: /Users/tfalade/.bash_profile: line 3: syntax error near unexpected token `then’
-bash: /Users/tfalade/.bash_profile: line 3: ` if [ $# -ne 0 ]; then’
Is there something that I’m doing wrong?
I’m getting this error when I ran the function:
Babatundes-iMac:~ tfalade$ source ~/.bash_profile
-bash: /Users/tfalade/.bash_profile: line 3: syntax error near unexpected token `then’
-bash: /Users/tfalade/.bash_profile: line 3: ` if[ $# -ne 0 ]; then’
Please help
Awesome, works like a charm! I forgot about changing the PATH, I typically just jet JAVA_HOME, but that stopped working recently (in some cases), since there very symlinks like /usr/bin/java ->
Thanks for the script. Works as it is described!
Many thanks. Works a treat!!
Thanks ! Work great !
thank’s
Pingback: itemprop="name">Grunt, Karma, Webpack und Co. - Toolchains für JavaScript-Projekte - codecentric Blog : codecentric Blog
Thanks for this, it work for me.
Just setjdk 1.7 should not include in script
thnx a lot.
Working great in 2018 on Sierra!