简体   繁体   中英

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?

Turns out java has been moved into brew core recently, so the correct command as of January 2021 is:

brew install java

Don't be like me and follow old instructions to install from cask, it will not work. You will get an error message from brew like this:

% brew install --cask java
Error: Cask 'java' is unavailable: No Cask with this name exists.

You may also see this:

% brew cask install java
Error: Calling brew cask install is disabled! Use brew install [--cask] instead.

As an add-on to the accepted answer: to install a certain version of Java, eg version 11, run:

brew install openjdk@11

And symlink it:

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

I had to sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

If you want to install the Oracle version of java's SDK with homebrew, use the following command:

brew install --cask oracle-jdk

If you don't care, then use the accepted answer by @Tim Fulmer to get the OpenJDK version

brew install java

Assembled from the answers here and How to set or change the default Java (JDK) version on macOS? :

You can use brew to install multiple java versions and run a command to switch between the versions as required.

Example

Install two java versions (change java versions as pleased):

brew install openjdk@19
brew install openjdk@8

Use the following command to see the installed versions:

 /usr/libexec/java_home -V

you should see the two versions specified in the response (if not, read further to create a symlink). Now you can select the java version using:

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

verify selected version:

java -version

you can further export the JAVA_HOME variable in your shell init file as speciifed in the attached SOF thread.

Now, in case you do not see the java version in /usr/libexec/java_home as expected and the version selection of that missing version is not working, you might need to add a symlink :

Executing brew info openjdk@the-missing-java-version should return the location of the installed version and will specify a symlink command that you should run for the system to find the SDK. The response text look something similar to:

...For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk...

run

brew reinstall openjdk

and it will display the following which shows your file path:

    For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

openjdk is keg-only, which means it was not symlinked into /usr/local,
because macOS provides similar software and installing this software in
parallel can cause all kinds of trouble.

If you need to have openjdk first in your PATH, run:
  echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> /Users/gerarddonnelly/.bash_profile

For compilers to find openjdk you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk/include"

I then ran the code below, which I took from the output above:

sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

The other issue I had was that Elasticsearch was not recognising my JAVA version. To fix that I added the following line to my.bash_profile

EXPORT JAVA_HOME=$(/usr/libexec/java_home) 

then I ran

source ~/.bash_profile 

to refresh my profile.

After that it worked.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM