macos - How to brew install java? - Stack Overflow
How to install Java on Macbook
macos - How to install Java on a Mac - Stack Overflow
Java Dev switching to Mac for the first time
Videos
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
As an add-on to the accepted answer: to install a certain version of Java, e.g. 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
In 2022, you can use just brew
brew install openjdk
and maybe you need to update PATH env:
export PATH="/usr/local/opt/openjdk/bin:$PATH"
…and for the future give a try to sdkman, is better than brew
curl -s "https://get.sdkman.io" | bash
then open a new shell and try list to see what you could install ;-)
sdk list java
At time of writing you could use:
sdk install java 17.0.1-tem
I have been doing some research on how to install Java jre and jdk on correct PATH.
JRE has been discontinued since Java 11. Since you are talking about Java 14, there is no JRE for it. You just need to install (and configure, if required) JDK.
Use the following command and then check java -version:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home"
export PATH="$PATH:$JAVA_HOME/bin"
Note that it will work only for your current session i.e. java -version won't work in another terminal window.
To set JAVA_HOME permanently, do as follows:
$ cd ~
$ vi .bash_profile
Write the following line into .bash_profile file, save and quit:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home"
export PATH="$PATH:$JAVA_HOME/bin"
Then refresh (read and execute .bash_profile)
$ source .bash_profile
And finally test
$ echo $JAVA_HOME
$ java -version
Note: Also, This thread may be useful to you.