Mac OS X and multiple Java versions

315,989

Solution 1

The cleanest way to manage multiple java versions on Mac is to use Homebrew.

And within Homebrew, use:

  • homebrew-cask to install the versions of java
  • jenv to manage the installed versions of java

As seen on http://hanxue-it.blogspot.ch/2014/05/installing-java-8-managing-multiple.html , these are the steps to follow.

  1. install homebrew
  2. install homebrew jenv
  3. install homebrew-cask
  4. install a specific java version using cask (see "homebrew-cask versions" paragraph below)
  5. add this version for jenv to manage it
  6. check the version is correctly managed by jenv
  7. repeat steps 4 to 6 for each version of java you need

homebrew-cask versions

Add the homebrew/cask-versions tap to homebrew using:

brew tap homebrew/cask-versions

Then you can look at all the versions available:

brew search java

Then you can install the version(s) you like:

brew cask install java7
brew cask install java6

And add them to be managed by jenv as usual.

jenv add <javaVersionPathHere>

I think this is the cleanest & simplest way to go about it.


Another important thing to note, as mentioned in Mac OS X 10.6.7 Java Path Current JDK confusing :

For different types of JDKs or installations, you will have different paths

You can check the paths of the versions installed using /usr/libexec/java_home -V, see How do I check if the Java JDK is installed on Mac?

On Mac OS X Mavericks, I found as following:

1) Built-in JRE default: /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

2) JDKs downloaded from Apple: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/

3) JDKs downloaded from Oracle: /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home


Resources

Solution 2

Uninstall jdk8, install jdk7, then reinstall jdk8.

My approach to switching between them (in .profile) :

export JAVA_7_HOME=$(/usr/libexec/java_home -v1.7)
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_9_HOME=$(/usr/libexec/java_home -v9)

alias java7='export JAVA_HOME=$JAVA_7_HOME'
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java9='export JAVA_HOME=$JAVA_9_HOME'

#default java8
export JAVA_HOME=$JAVA_8_HOME

Then you can simply type java7 or java8 in a terminal to switch versions.

(edit: updated to add Dylans improvement for Java 9)

Solution 3

For macOS Sierra 420

This guide was cobbled together from various sources (replies above as well as other posts), and works perfect.

0. If you haven't already, install homebrew.

See https://brew.sh/

1. Install jenv

brew install jenv

2. Add jenv to the bash profile

if which jenv > /dev/null; then eval "$(jenv init -)"; fi

3. Add jenv to your path

export PATH="$HOME/.jenv/shims:$PATH"

4. Tap "caskroom/versions"

FYI: "Tap" extends brew's list of available repos it can install, above and beyond brew's default list of available repos.

brew tap caskroom/versions

5. Install the latest version of java

brew cask install java

6. Install java 6 (or 7 or 8 whatever you need)

brew cask install java6
#brew cask install java7
#brew cask install java8

? Maybe close and restart Terminal so it sees any new ENV vars that got setup.

7. Review Installations

All Java version get installed here: /Library/Java/JavaVirtualMachines lets take a look.

ls -la /Library/Java/JavaVirtualMachines

8. Add each path to jenv one-at-a-time.

We need to add "/Contents/Home" to the version folder. WARNING: Use the actual paths on your machine... these are just EXAMPLE's

jenv add /Library/Java/JavaVirtualMachines/1.6.0___EXAMPLE___/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk___EXAMPLE___/Contents/Home

9. Check if jenv registered OK

jenv versions

10. Set java version to use (globably)

Where XX matches one of the items in the versions list above.

jenv global XX

Check java version

java -version

Check jenv versions

Should also indicate the current version being used with an asterisk.

jenv versions

DONE


Quick future reference

To change java versions

... See the list of available java versions

jenv versions

... then, where XX matches an item in the list above

jenv global XX

Solution 4

SDKMAN! is a great tool for using multiple versions of Java, Gradle, Groovy, Kotlin, and other JVM tools on Mac OS. Installation and usage doc are easily found on the main site.

(I have no affiliation, just a happy user).

As an example usage, if I type the following in a Terminal window, there is a list of available Java SDK versions (edited for brevity):

$ sdk list java
Available Java Versions
   + 9ea170                                                                        
 > + 8u131                                                                         
     7u141-zulu                     

Here + denotes that the version is installed. > denotes which version is currently in use. To install a version:

$ sdk install java 7u141-zulu

To use a version in this Terminal window:

$ sdk use java 9ea170

Solution 5

First, you need to make certain you have multiple JAVA versions installed. Open a new Terminal window and input:

/usr/libexec/java_home -V

Your output should look like:

Matching Java Virtual Machines (2):
11.0.1, x86_64: "Java SE 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
1.8.0_201, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home

Note that there are two JDKs available. If you don’t notice the Java version you need to switch to, download and install the appropriate one from here (JDK 8 is represented as 1.8) . Once you have installed the appropriate JDK, repeat this step.

  1. Take note of the JDK version you want to switch to. For example, “11.0” and “1.8” are the JDK versions available in the example above.

  2. Switch to the desired version. For example, if you wish to switch to JDK 8, input the following line:

    export JAVA_HOME=/usr/libexec/java_home -v 1.8

For 11.0, switch “1.8” with “11.0” 4. Check your JDK version by inputting into Terminal:

java -version

If you have followed all the steps correctly, the JDK version should correlate with the one you specified in the last step. 5. (Optional) To make this the default JDK version, input the following in Terminal:

open ~/.bash_profile

Then, add your Terminal input from step 3 to this file:

SWITCH TO JAVA VERSION 8

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

Save and close the file.

Share:
315,989
Dakkar
Author by

Dakkar

Updated on December 29, 2021

Comments

  • Dakkar
    Dakkar over 2 years

    How can I install an additional java on MacOS? I installed jdk8 and that works fine. But now I need a jdk7 installation for development purposes. When trying to install the old version via DMG file, i get a warning, that there is already a newer version of java installed and the installer quits.

        /usr/libexec/java_home -verbose
        Matching Java Virtual Machines (1):
            1.8.0_20, x86_64:   "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home
    
           /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home
    

    How to install jdk7 in addition to this one?

  • fill_J
    fill_J almost 9 years
    So far this is the best option for me. easily can switch to any version with just a simple command "java8" or "java7". Thanks for the help
  • Unnawut
    Unnawut almost 9 years
    It would be good to mention that homebrew-cask can be installed with command brew install caskroom/cask/brew-cask instead of brew install cask which is an Emacs plugin.
  • Andrea.cabral
    Andrea.cabral over 8 years
    Do we have to delete the current installed version of Java (1.8), and reinstalling it with brew to be able to brew-manage both?
  • Adrien Be
    Adrien Be over 8 years
    Don't think you have to. As mentioned in the last step of the installation process, use jenv add <javaVersionPathHere> to let jenv know where it can find a given java version.
  • Sammy Kumar
    Sammy Kumar over 8 years
    I just wanted to mention, since it wasn't working for me. brew cask install java7 downloads the package file but doesn't install it. You still need manually install it. Then you should be able to add the path to jenv Also, brew cask search java doesn't return java8 for some reason.
  • Nico
    Nico over 8 years
    Great ! :) And for other like me you can use : ls /Library/Java/JavaVirtualMachines to see which versions are available. Then you can switch on different 1.8.x versions. And also you have to add export PATH=$JAVA_HOME/bin:$PATH
  • Nico
    Nico over 8 years
    @squid267 the cleanest way is to avoid homebrew. Try Vegard solution, I added a comment that should help you (but before download and install by yourself the jdk or jre you want oracle.com/technetwork/java/javase/downloads/index.html)
  • Adrien Be
    Adrien Be over 8 years
    @squid267 did you open a ticket on Github on Homebrew Cask's repo? I doubt this is a normal behavior.
  • Adrien Be
    Adrien Be over 8 years
    @Nico Why would you avoid a generic solution that was contributed by 100 people since April 2012 & tested by 10s of thousands? it has 2,315 Closed vs 64 Opened issues... just look at the repo, numbers speak for themselves, it's a pretty damn good tool
  • Nico
    Nico over 8 years
    @AdrienBe I tried homeBrew two years ago and I encountered several problems when I wanted to change for example mysql versions, then I decided to do things by my own. But you must be right, maybe today this tool is better. I think it's a choice, for people confortable with mac os command lines and system administration maybe avoid homebrew is better in order to keep a better view of what is installed.
  • Adrien Be
    Adrien Be over 8 years
    @Nico yeah, in the end one should just try out & choose whatever (s)he feels comfortable with. Some tools are just irrelevant if you have the relevant skills already, sys admin stuff in this case. Although one could actually like this as it avoids you having to do repetitive work...
  • Titus
    Titus almost 8 years
    Thanks for this one. I just wanted to mention that I've managed to do this by installing jdk7 after jdk8 (there is no need to uninstall jdk8).
  • Seki
    Seki almost 8 years
    Do not put images of your commands, it makes difficult to copy/paste them. To highlight commands, use the backticks "`" for a single word or a short command, or indent your command with 4 spaces on its own line (or multiples of 4 in lists).
  • Dinesh Arora
    Dinesh Arora almost 8 years
    Thanks Seki. I was struggling to post images and spent a long time trying to format but could not get it right. Thanks for sharing the backticks info.
  • juil
    juil almost 8 years
    I ran into a few problems while implementing this solution, one of which was with jenv returning the No such file or directory. error. This wiki helped solve it for me. github.com/gcuisinier/jenv/wiki/Trouble-Shooting
  • Nikolay Tsenkov
    Nikolay Tsenkov over 7 years
    I would add that all java installs would be in /Library/Java/JavaVirtualMachines/ and when adding them with jenv add you add path looking like this /Library/Java/JavaVirtualMachines/[specific-version]/Content‌​s/Home/. Cheers!
  • matt burns
    matt burns over 7 years
    @SammyKumar I think java8 isn't listed because 8 is what you get by default when you just use brew cask install java
  • Mingliang Liu
    Mingliang Liu about 7 years
    As of Jun 23 2017, I run brew cask search java7 but got No Cask found for "java7".
  • Adrien Be
    Adrien Be about 7 years
    @MingliangLIU yeah me too... it really sucks. I found issues about this on Github were they mentioned some work arounds but I could not get it to work. So I ended up having to do it "the manual way" via apple and oracle's "Java versions download and install" web pages. Then using /usr/libexec/java_home -V to double check the paths where these were installed.
  • Mingliang Liu
    Mingliang Liu about 7 years
    @AdrienBe Thanks for sharing this. As this is confirmed, I'll also install manually. Hadoop 2 is still using Java 7, unfortunately...
  • cvakiitho
    cvakiitho almost 7 years
    Dunno why I wasted my time with jenv, as this just works, and does not clutter your shell startup by 0.5s with jenv init.
  • PJSCopeland
    PJSCopeland over 6 years
    Step 2.5. $ eval "$(jenv init -)" as per linked page.
  • Dylan Nissley
    Dylan Nissley over 6 years
    If you are trying to get java 9 working in this fashion, the version argument to java_home for java 9 should simply be 9 e.g. export JAVA_9_HOME=$(/usr/libexec/java_home -v9)
  • Jay
    Jay over 6 years
    is there a way to install a specific version with Java8. For eg 1.8.0_151-b12 or older within Java 8? thanks
  • Aykut Akıncı
    Aykut Akıncı over 6 years
    sdkman supports versions starting with java 1.7. If you're willing to use java 1.6 this solution may not be suitable for you.
  • ryanlutgen
    ryanlutgen over 6 years
    Thanks for this! Had Java8 installed with homebrew already, installed 9 as well, did this (sans jdk7) and it seems to be working.
  • Ellen Spertus
    Ellen Spertus about 6 years
    I think you may have used the same image four times.
  • jrhee17
    jrhee17 about 6 years
    I really want this to work.. but unfortunately I couldn't find a way to use openjdk..
  • Eduardo Dennis
    Eduardo Dennis about 6 years
    sdkman is great, but they dont support specific releases within a version. For example Datastax Dev Center only works with 1.8.0_151 but I can't specify that with sdkman. Hopefully they add more versions.
  • Anthony Fammartino
    Anthony Fammartino about 6 years
    If you have a local version, I think you can install it into SDKMan - sdkman.io/usage#localversion (but I haven't tried it)
  • TastyCode
    TastyCode about 6 years
    great solution!
  • Oliver Hernandez
    Oliver Hernandez almost 6 years
    This should be the accepted answer, much simpler than installing extraneous software, especially if you're using a corporate managed Mac where there are restrictions on software that can be installed.
  • Eugen Mayer
    Eugen Mayer almost 6 years
    @AdrienBe you should update your answer with the current ways to install java, which is cask "brew cask install java8" "brew cask install java-beta" "brew cask install java" "brew cask install java7" .. there is no tap anymore. changed a lot. Thanks for the brief answer
  • terencey
    terencey over 5 years
    Additionally, restart your terminal after brew cask install java*
  • badsyntax
    badsyntax over 5 years
    Along similar lines, you can also use a tool like direnv to specify Java versions per project so you don't need to remember to switch yourself. All it does it automatically set JAVA_HOME. I've outlined this approach here: gist.github.com/badsyntax/4c66a0f09f545320e52bb4a559777a88
  • Ritesh Singh
    Ritesh Singh over 5 years
    Clear and great explanation step by step.
  • Alexey Sh.
    Alexey Sh. almost 5 years
    Error: Cask 'java8' is unavailable: No Cask with this name exists.
  • supi
    supi almost 5 years
    java8/9/10 arent available anymore as casks. Try this instead: " brew cask install adoptopenjdk/openjdk/adoptopenjdk8 "
  • cakraww
    cakraww almost 5 years
    By default jenv does not set JAVA_HOME environment variable. You need to enable the plugin by jenv enable-plugin export.
  • Vyacheslav Cotruta
    Vyacheslav Cotruta almost 5 years
    brew tap caskroom/versions should be brew tap homebrew/cask-versions
  • HankCa
    HankCa over 4 years
    Great work! Even better than steps 7, 8 use - /usr/libexec/java_home -V and then jenv add <what that returns> or you could script it - for ver in $(/usr/libexec/java_home -V 2>&1 >/dev/null | grep -v Matching | awk '{print $1}'); do echo X: $(jenv add $(/usr/libexec/java_home -v$ver)); done
  • HankCa
    HankCa over 4 years
    The 2>&1 >/dev/null is because the versions from java_home are printed to STDERR and the actual version being used is printed to STDOUT.
  • Anish B.
    Anish B. over 4 years
    @Vegard Wonderful. Thanks +1 for this.
  • willrochathomas
    willrochathomas over 4 years
    unless I'm mistaken, the alias commands shouldn't have backticks (`) around them but instead single quotes ('). Otherwise the alias just gets set to the output of the export command (blank) rather than the export command itself
  • Vegard
    Vegard over 4 years
    Yes, that was a late edit from someone else, I'll revert. Thanks @willrochathomas.
  • pob
    pob about 4 years
    Great instructions; I'm on osx 10.15.3; step 3 above duplicates step 2; also, /Library/Java/Extensions was working for Java SE 8, but doesn't work for adoptopenjdk-9, so I use "java -cp 'path_to_jars'" instead, which is awkward.
  • John Red
    John Red about 4 years
    This is the answer guys. /usr/libexec/java_home is awesome. Read about it a bit more. It's all you need.
  • antimatter
    antimatter about 4 years
    It took awhile for me to figure out the path to Java Home for the instruction, jenv add <javaVersionPathHere> . If you put the command, /usr/libexec/java_home -V just below it would be a good enhancement.
  • Taoufik Mohdit
    Taoufik Mohdit over 3 years
    When running jenv add <>, if you get an error like this : ln: ~/.jenv/versions/openjdk64-11.0.8: No such file or directory, then it means .jenv/versions folder is missing; creating one (mkdir ~/.jenv/versions), then re-running jenv add <> should solve the problem
  • msilb
    msilb over 3 years
    If you are on Big Sur, make sure to follow the advice outlined in this answer. Looks like Big Sur broke the way usr/libexec/java_home worked in previous versions, so setting JAVA_HOME in advance won't work.
  • Minh Nghĩa
    Minh Nghĩa over 3 years
    For Homebrew 2.6.0+, replaces cask install with install --cask.
  • Exploring
    Exploring about 3 years
    what is the difference between sdkman and jenv?
  • bdzzaid
    bdzzaid about 3 years
    @Exploring jenv is also a good option, sdkman include also kotlin, gradle, maven, ant, asciidotorJ and many other tools needed for a developer
  • NeilG
    NeilG almost 3 years
    $ brew install --cask java8 now gives Error: Cask 'java8' is unavailable: No Cask with this name exists. brew search java actually lists java6 but neither java7 nor java8!
  • NeilG
    NeilG almost 3 years
    After trying a couple of other commands this one worked; be prepared to have a github login and your admin account password ready: brew install --cask homebrew/cask-versions/adoptopenjdk8
  • NeilG
    NeilG almost 3 years
    Here's the stuff about searching through the different places brew might store your Java8, spelled out in more detail: dev2qa.com/…
  • Adelin
    Adelin almost 3 years
    is this still valid with Big Sur OS ?
  • Vegard
    Vegard over 2 years
    @msilb apparently this was caused by a bug that has later been fixed.
  • Askar
    Askar about 2 years
    If you use zsh, then add aforementioned exports on ~/.zprofile
  • RobLW
    RobLW about 2 years
    This was the perfect solution for me. thank you. I wasn't happy with the way jenv worked (or lack of working in my case) but found this to be sdkman to get the job done for me allowing me to run LSP-Metal in sublime 3 and code scala.
  • jskattt797
    jskattt797 almost 2 years
    Did you mean to include a link with "download and install the appropriate one from here"?