Explain Codes LogoExplain Codes Logo

How to brew install java?

java
prompt-engineering
java-8
venv
Nikita BarsukovbyNikita Barsukov·Dec 13, 2024
TLDR
# Because who has time for verbose commands, right? brew install openjdk@11

Confirm the successful installation with java -version. If the system can't find Java, you might need to create a symlink.

# Give the system a clue, 404 Java not found sudo ln -sfn /usr/local/opt/openjdk@11 /Library/Java/JavaVirtualMachines/openjdk-11.jdk

In need of the latest and greatest Java? The following will do:

# Riding the wave of the most recent Java brew install java

Have system Java wrappers? Create this symlink to make it work:

# Symlink, or the breadcrumbs for your system to find Java sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

Validate your efforts with java -version.

Specify your Java version

Do you have a favorite Java version? Here's how to install it:

# Because we all have preferences, right? brew install openjdk@8

Feel free to replace openjdk@8 with openjdk@11 or your particular preference.

Juggling multiple versions of Java

You want to see all installed Java versions? Here's how:

# The Java beauty contest /usr/libexec/java_home -V

Set a specific Java version as default this way:

# King Java: Long live the king! export JAVA_HOME=$(/usr/libexec/java_home -v version_number)

But wait, there's more! Remember to update your shell initialization files (.bashrc or .zshrc) with the export command for a permanent solution.

Mitigating common Java issues

Here's a handy guide to fixing classic Java issues.

  1. Error: cask "adoptopenjdk" is unavailable. This can be easily rectified by turning to the Eclipse Temurin build:
# AdoptOpenJDK left the chat. Welcome, temurin! brew install --cask temurin
  1. Conflicting Java versions can be solved by using jenv to seamlessly switch between them. Here's a quick guide to wield jenv like a pro:
# Manage Java like a pro with jenv brew install jenv echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc echo 'eval "$(jenv init -)"' >> ~/.zshrc source ~/.zshrc jenv add /Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home
  1. Incorrect JAVA_HOME after updating? Refresh it with this command:
# Because JAVA_HOME needs an occasional spa day too export JAVA_HOME=$(/usr/libexec/java_home -v version_number)

Always remember to add it to your .zshrc or .bash_profile to make it stick.