[java] Mac OS X and multiple Java versions

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?

Thanks
Dakky

This question is related to java macos multiple-versions homebrew-cask jenv

The answer is


To install more recent versions of OpenJDK, I use this. Example for OpenJDK 14:

brew info adoptopenjdk
brew tap adoptopenjdk/openjdk
brew cask install adoptopenjdk14

See https://github.com/AdoptOpenJDK/homebrew-openjdk for current info.


Jenv on Mac Sierra:

if not working after install, do this bug fix to add java executable to path

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

even though eval "$(jenv init -)" could do this job. The reason is /bin folder is not there anymore as describe in it's homepage, but shim folder is used as /bin instead.

  • Make sure ~/.jenv is there
  • which java may print /Library/...
  • jenv global 1.8
  • jenv shell 1.8

Eventually, which java gives you:

/Users/xxxx/.jenv/shims/java


I followed steps in below link - https://medium.com/@euedofia/fix-default-java-version-on-maven-on-mac-os-x-156cf5930078 and it worked for me.

cd /usr/local/Cellar/maven/3.5.4/bin/
nano mvn
--Update JAVA_HOME -> "${JAVA_HOME:-$(/usr/libexec/java_home)}"
mvn -version

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)


Here's a more DRY version for bash (Based on Vegard's answer)

Replace 1.7 and 1.8 with whatever versions you are interested with and you'll get an alias called 'javaX'; where 'X' is the java version (7 / 8 in the snippet below) that will allow you to easily switch versions

for version in 1.7 1.8; do
    v="${version: -1}"
    h=JAVA_"$v"_HOME

    export "$h"=$(/usr/libexec/java_home -v $version)

    alias "java$v"="export JAVA_HOME=\$$h"
done

I answer lately and I really recommand you to use SDKMAN instead of Homebrew.

With SDKMAN you can install easily different version of JAVA in your mac and switch from on version to another.

Java in your mac

You can also use SDKMAN for ANT, GRADLE, KOTLIN, MAVEN, SCALA, etc...

To install a version in your mac you can run the command sdk install java 15.0.0.j9-adpt cmd


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.


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 plainly 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

Manage multiple java version in MAC using jenv

  1. Install homebrew using following command

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

  1. install jenv and activate jenv

brew install jenv

echo 'eval "$(jenv init -)"' >> ~/.bash_profile

  1. tap cask-versions

brew tap homebrew/cask-versions

  1. search available java version that can be installed

brew search java

  1. E.g. to install java6 use following command

brew install cask java6

  1. Add multiple versions of java in jenv

jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home

jenv add /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

Note:- if you get error like “”ln: /Users//.jenv/versions/oracle64-1.8.0.231: No such file or directory, then run following:-

mkdir -p /Users//.jenv/versions/oracle64-1.8.0.231

  1. Rehash jenv after adding jdk’s

jenv rehash

  1. List known versions of java to jenv

jenv versions

  1. Set default version

jenv global oracle64-1.8.0.231

  1. Change java version for a project

jenv local oracle64-1.6.0.65

  1. set JAVA_HOME with the same version as jenv

jenv exec bash

echo $JAVA_HOME


I find this Java version manager called Jabba recently and the usage is very similar to version managers of other languages like rvm(ruby), nvm(node), pyenv(python), etc. Also it's cross platform so definitely it can be used on Mac.

After installation, it will create a dir in ~/.jabba to put all the Java versions you install. It "Supports installation of Oracle JDK (default) / Server JRE, Zulu OpenJDK (since 0.3.0), IBM SDK, Java Technology Edition (since 0.6.0) and from custom URLs.".

Basic usage is listed on their Github. A quick summary to start:

curl -sL https://github.com/shyiko/jabba/raw/master/install.sh | bash && . ~/.jabba/jabba.sh

# install Oracle JDK
jabba install 1.8 # "jabba use 1.8" will be called automatically  
jabba install 1.7 # "jabba use 1.7" will be called automatically 

# list all installed JDK's
jabba ls

# switch to a different version of JDK
jabba use 1.8

I am using Mac OS X 10.9.5. This is how I manage multiple JDK/JRE on my machine when I need one version to run application A and use another version for application B.

I created the following script after getting some help online.

#!bin/sh
function setjdk() {
  if [ $# -ne 0 ]; then
   removeFromPath '/Library/Java/JavaVirtualMachines/'
   if [ -n "${JAVA_HOME+x}" ]; then
    removeFromPath $JAVA_HOME
   fi
   export JAVA_HOME=/Library/Java/JavaVirtualMachines/$1/Contents/Home
   export PATH=$JAVA_HOME/bin:$PATH
  fi
 }
 function removeFromPath() {
  export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
 }
#setjdk jdk1.8.0_60.jdk
setjdk jdk1.7.0_15.jdk

I put the above script in .profile file. Just open terminal, type vi .profile, append the script with the above snippet and save it. Once your out type source .profile, this will run your profile script without you having to restart the terminal. Now type java -version it should show 1.7 as your current version. If you intend to change it to 1.8 then comment the line setjdk jdk1.7.0_15.jdk and uncomment the line setjdk jdk1.8.0_60.jdk. Save the script and run it again with source command. I use this mechanism to manage multiple versions of JDK/JRE when I have to compile 2 different Maven projects which need different java versions.


New commands for installing Java via Homebrew:

  • brew cask install adoptopenjdk/openjdk/adoptopenjdk8
  • brew cask install adoptopenjdk/openjdk/adoptopenjdk11

See the homebrew-openjdk repo for the latest commands.

Installing Java

You can install Java via Homebrew, Jabba, SDKMAN or manually. See this answer for details on all the commands.

Switching Java versions*

You can switch Java versions with jenv Jabba, SDKMAN or manually. See details on all the switching commands here.

Best solutions

  • Jabba is designed to work on multiple platforms, so it's a good option if you want a solution that'll also work on Windows
  • Using Homebrew to download Java versions and jenv to switch versions provides a nice workflow. jenv makes it easy to work with Java versions stored in any directory on your machine, so it's a good alternative if you're interested in storing Java in non-default directories.
  • Using SDKMAN to download Javas and switch versions is another great alternative
  • Manually switching should be avoided because it's an unnecessary headache.

Function to manually switch Java versions

Here's the Bash / ZSH function for manually switching Java versions (by OpenJDK):

jdk() {
        version=$1
        export JAVA_HOME=$(/usr/libexec/java_home -v"$version");
        java -version
 }

There are great tools for switching Java versions, so I highly recommend against doing it manually.


In the same spirit than @Vegard (lightweight):

  • Install the wanted JDKs with Homebrew
  • Put this jdk bash function and a default in your .profile

    jdk() {
        version=$1
        export JAVA_HOME=$(/usr/libexec/java_home -v"$version");
        java -version
     }
    
    export JAVA_HOME=$(/usr/libexec/java_home -v11); # Your default version
    
  • and then, to switch your jdk, you can do

     jdk 9
     jdk 11
     jdk 13
    

Based on https://github.com/AdoptOpenJDK/homebrew-openjdk.


As found on this website So Let’s begin by installing jEnv

  1. Run this in the terminal

    brew install https://raw.github.com/gcuisinier/jenv/homebrew/jenv.rb
    
  2. Add jEnv to the bash profile

    if which jenv > /dev/null; then eval "$(jenv init -)"; fi
    
  3. When you first install jEnv will not have any JDK associated with it.

    For example, I just installed JDK 8 but jEnv does not know about it. To check Java versions on jEnv

    At the moment it only found Java version(jre) on the system. The * shows the version currently selected. Unlike rvm and rbenv, jEnv cannot install JDK for you. You need to install JDK manually from Oracle website.

  4. Install JDK 6 from Apple website. This will install Java in /System/Library/Java/JavaVirtualMachines/. The reason we are installing Java 6 from Apple website is that SUN did not come up with JDK 6 for MAC, so Apple created/modified its own deployment version.

  5. Similarly install JDK7 and JDK8.

  6. Add JDKs to jEnv.

    JDK 6:

    JDK 7: http://javahabi@javahabit.com/wp-content/uploads/2015/03/img_5518ab9bc47d4.png

    JDK 8: http://javahabi@javahabit.com/wp-content/uploads/2015/03/img_5518abb2c1217.png

  7. Check the java versions installed using jenv

    http://javahabi@javahabit.com/wp-content/uploads/2015/03/img_5518abceb0deb.png

  8. So now we have 3 versions of Java on our system. To set a default version use the command

    jenv local <jenv version>
    

    Ex – I wanted Jdk 1.6 to start IntelliJ

    jenv local oracle64-1.6.0.65
    
  9. check the java version

    java -version http://javahabi@javahabit.com/wp-content/uploads/2015/03/img_5518abe376dd0.png

That’s it. We now have multiple versions of java and we can switch between them easily. jEnv also has some other features, such as wrappers for Gradle, Ant, Maven, etc, and the ability to set JVM options globally or locally. Check out the documentation for more information.


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

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"

Examples related to multiple-versions

Mac OS X and multiple Java versions How to pip install a package with min and max version range?

Examples related to homebrew-cask

How to Install Sublime Text 3 using Homebrew Mac OS X and multiple Java versions

Examples related to jenv

Mac OS X and multiple Java versions