Search code examples
javamacoshomebrew

How to brew install java?


I'd like to setup java on a new OS X machine, and prefer to use brew for OS X package management. How can I install latest java using brew?


Solution

  • Turns out java has been moved into brew core recently, so the correct command as of August 2022 is:

    brew install java
    

    Then check your installation by running

    java -version
    

    If the result does not looks like this:

    openjdk 18.0.2 2022-07-19
    OpenJDK Runtime Environment Homebrew (build 18.0.2+0)
    OpenJDK 64-Bit Server VM Homebrew (build 18.0.2+0, mixed mode, sharing)
    

    but like this:

    The operation couldn’t be completed. Unable to locate a Java Runtime.
    Please visit http://www.java.com for information on installing Java.
    

    Then you also need to create a symlink for the system Java wrappers to find this JDK:

    sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk \
         /Library/Java/JavaVirtualMachines/openjdk.jdk