Skip to content

Instantly share code, notes, and snippets.

@tomysmile
Last active September 30, 2024 19:44
Show Gist options
  • Save tomysmile/a9a7aee85ff73454bd57e198ad90e614 to your computer and use it in GitHub Desktop.
Save tomysmile/a9a7aee85ff73454bd57e198ad90e614 to your computer and use it in GitHub Desktop.
How To Install Java 8 on Mac

Install HomeBrew first

brew update
brew tap caskroom/cask
brew install brew-cask

If you get the error "already installed", follow the instructions to unlink it, then install again:

brew unlink brew-cask
brew install brew-cask

upgrade brew:

brew update && brew upgrade brew-cask && brew cleanup && brew cask cleanup

Install Java 7 and 8

People on the Stackoverflow cautioned not to install 8 until 7 is installed. So we are going to install JDK 7 first.

Unlike other version managers such as NVM, jEnv itself doesn’t install JDKs. You have to do it yourself. Luckily, Homebrew Cask made this task really easy. But before doing that, let’s check if we already have JDK 7 installed by Homebrew Cask:

brew tap caskroom/versions
brew cask info java7

install Java 7:

brew cask install java7

If you run into permission issue, add sudo at the beginning of the above command.

As of today, Java 8 is the latest stable. Run the following command to install Java 8:

brew cask install java

These two JDKs will be installed at the following directories. Your JDKs’ minor and patch versions might be different.

/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/
/Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home/

Enter jEnv

Now it is time to install jEnv:

brew install jenv

Add the following lines to ~/.bash_profile. This will initialize jEnv.

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

jEnv doesn’t install JDKs, so we have to tell jEnv where to look for them. Type these commands to register JDKs in jEnv (replace the minor and patch versions with yours):

jenv add /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/
jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home/

After that, run this command to list all registered JDKs:

jenv versions

The version with an asterisk is the active version.

In my case, I need to keep JDK 7 as my default version, so I set the global version to 1.7:

jenv global oracle64-1.7.0.79

And in my project, I set the local JDK version to 1.8:

cd <my project>
jenv local oracle64-1.8.0.66

The above command will create a .java-version file at project root. Its content is the version I just picked for this project:

oracle64-1.8.0.66

@AskMarvinV
Copy link

i tried this instruction and it's missing this on the profile.

export JENV_ROOT=/usr/local/var/jenv

it needs to be added in order to work else you'll be getting /.jenv/versions/oracle64-1.7.0.80: No such file or directory

@alChaCC
Copy link

alChaCC commented Nov 22, 2016

Hi @vcollab
I fixed by mkdir -p ~/.jenv/versions

@drmjc
Copy link

drmjc commented Jan 17, 2017

Thanks, great write up!

@vin0010
Copy link

vin0010 commented Feb 6, 2017

When I run "brew cask info java7" its automatically installing "jdk1.7.0_80" but I need "jdk1.7.0_79". How can I do that?

@timse7
Copy link

timse7 commented Nov 13, 2017

java7 is not available (anymore?) afaik. With brew cask search java it's possible to search for what's available related to java

@Chessray
Copy link

The instructions worked for me, but I had to call the jenv add commands using sudo.

@ssstonebraker
Copy link

i had to install java 7 like this:

brew cask install caskroom/versions/zulu7

@bih
Copy link

bih commented Dec 8, 2019

This is updated to

$ brew cask install homebrew/cask-versions/zulu8

@ramaro-meli
Copy link

ramaro-meli commented Sep 2, 2020

now is

brew cask install homebrew/cask-versions/zulu7
brew cask install homebrew/cask-versions/zulu8

@abdullah-shiwani
Copy link

For OpenJDK-8

brew tap adoptopenjdk/openjdk

brew cask install adoptopenjdk/openjdk/adoptopenjdk8

Installation Path
/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin

Verify

cd /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin

./java -version

Output

openjdk version "1.8.0_265"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_265-b01)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.265-b01, mixed mode)

@buuug7
Copy link

buuug7 commented Jan 24, 2021

For OpenJDK-8

brew tap adoptopenjdk/openjdk

brew cask install adoptopenjdk/openjdk/adoptopenjdk8

Installation Path
/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin

Verify

cd /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin

./java -version

Output

openjdk version "1.8.0_265"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_265-b01)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.265-b01, mixed mode)

this is not work with the latest homebrew, simple type

brew install adoptopenjdk8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment