Skip to content

Instantly share code, notes, and snippets.

@leifericf
Last active June 3, 2024 16:14
Show Gist options
  • Save leifericf/f61c60a9ab889ed752f969e9db2009a0 to your computer and use it in GitHub Desktop.
Save leifericf/f61c60a9ab889ed752f969e9db2009a0 to your computer and use it in GitHub Desktop.
Installing Java on macOS and Adding It to jEnv

Install Homebrew:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Install jEnv using Homebrew:

brew install jenv

Add the cask-versions tap to Homebrew:

brew tap homebrew/cask-versions

Install Temurin 21 (formerly AdoptOpenJDK):

brew install --cask temurin21

Install other Java versions if necessary:

brew install --cask temurin8

Make these Java versions known to jEnv:

jenv add /Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home/

jenv add /Library/Java/JavaVirtualMachines/temurin-8.jdk/Contents/Home/

Use jEnv to set the global version of Java:

jenv global 21.0.1

Verify that jEnv is aware of these Java versions:

jenv versions

The output should look something like this:

  system
  1.8
  1.8.0.392
  21.0
  21.0.1
  temurin64-1.8.0.392
* temurin64-21.0.1 (set by /Users/<user>/.jenv/version)

The star (*) indicates that we have successfully set temurin64-21.0.1 globally.

We can set a local version of Java for the current working directory:

jenv local 1.8

This will create a file called .java-version which specifies the Java version to use for this directory.

We can check which version of Java is active:

java --version

The output should look something like this:

openjdk 21.0.1 2023-10-17 LTS
OpenJDK Runtime Environment Temurin-21.0.1+12 (build 21.0.1+12-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.1+12 (build 21.0.1+12-LTS, mixed mode)

Note that, for Java 8, we must use java -version (with one -):

java -version

See this Stack Overflow answer for more details.

@leifericf
Copy link
Author

SDKMAN is another viable alternative to jEnv.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment