Search code examples
macosinstallationjava-17java-home

How to install java 17 on Mac using brew and setting up java_home?


Install java 17 on Mac using brew and setting up java_home

I tried this:

brew install java17  

> Downloading https://formulae.brew.sh/api/formula.jws.json
  100\.0%
> Downloading https://formulae.brew.sh/api/cask.jws.json
  100\.0%
> Warning: No available formula with the name "java17". Did you mean javacc?
> Searching for similarly named formulae and casks...
> Error: Cask java6 exists in multiple taps:
  homebrew/cask-versions/java6
> caskroom/versions/java6

Solution

  • Assuming brew is already installed and updated:

    brew install openjdk@17
    

    If using bash:

    echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile
    

    Or using zsh (on macOS Catalina or newer), then it should be:

    echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.zshrc
    

    Restart your shell.

    If you have multiple JDK versions installed, you can specify one, using -v flag:

    echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 17)" >> ~/.zshrc
    

    Check the java version:

    java --version
    
    openjdk 17.0.2 2022-01-18
    OpenJDK Runtime Environment (build 17.0.2+8-86)
    OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)