Developer guide · Windows and Mac
How to change the default Java (JDK) version on a Mac
By Aqib Javed · Updated Nov 27, 2026
Run /usr/libexec/java_home -V to list your JDKs, add export JAVA_HOME=$(/usr/libexec/java_home -v 21) to ~/.zshrc and open a new terminal; the java command macOS provides follows JAVA_HOME, so java -version now shows 21. A Homebrew openjdk only shows up in that list after the symlink step brew prints. Android Studio, IntelliJ and Flutter choose their own JDK, so set those too. Stackyard, free, installs any JDK version and flags a Java too new for a Flutter app's Gradle.
Check which Java runs now
/usr/libexec/java_home -V lists every JDK installed in /Library/Java/JavaVirtualMachines with its version and path. /usr/bin/java, the java that comes with macOS, runs the JDK JAVA_HOME points to. With JAVA_HOME unset, it runs the newest JDK in that folder.
which -a java lists every java on your PATH in the order zsh searches them. If a folder such as /opt/homebrew/opt/openjdk/bin comes before /usr/bin, that java runs no matter what JAVA_HOME says.
java -version
which -a java
echo $JAVA_HOME
/usr/libexec/java_home -VHow to set the default Java version in ~/.zshrc
- Run /usr/libexec/java_home -V and note the major version you want, for example 21.
- Open ~/.zshrc in a text editor (open -e ~/.zshrc, or nano ~/.zshrc). zsh has been the default shell since macOS Catalina; if you use bash, edit ~/.bash_profile instead.
- Add the export line below. Remove any older JAVA_HOME line in ~/.zshrc or ~/.zprofile, so only one sets it.
- Save, then run source ~/.zshrc or open a new terminal window, and check with java -version.
-v 21 picks the newest installed 21.x. If no JDK 21 is installed, java_home doesn't complain: it prints the newest JDK instead, so you silently get another version. Add -F (java_home -F -v 21) to make it fail instead. To pin one exact build, set JAVA_HOME to its path from the -V list, ending in Contents/Home.
# ~/.zshrc
export JAVA_HOME=$(/usr/libexec/java_home -v 21)
# then
source ~/.zshrc
java -versionSwitch Java for one terminal or one command
An export typed in a window changes only that window. Close it and the default from ~/.zshrc is back. Putting JAVA_HOME= in front of a command changes it for that command alone, which is handy for building one old project with an older JDK.
# this terminal window only
export JAVA_HOME=$(/usr/libexec/java_home -v 17)
java -version
# one command only
JAVA_HOME=$(/usr/libexec/java_home -v 17) ./gradlew build
/usr/libexec/java_home -v 17 --exec javac -version
# shortcuts in ~/.zshrc: type j17 or j21
alias j17='export JAVA_HOME=$(/usr/libexec/java_home -v 17)'
alias j21='export JAVA_HOME=$(/usr/libexec/java_home -v 21)'Install and switch JDKs with Homebrew
- brew install openjdk@21 installs a JDK (openjdk with no number is the newest release). These formulae are keg-only: Homebrew doesn't link them onto your PATH, and java_home can't see them yet.
- To make java_home and /usr/bin/java find it, run the symlink command from brew's caveats (brew info openjdk@21 shows it again). After that, the JAVA_HOME line above switches to it like any other JDK.
- brew install --cask temurin@21 installs Eclipse Temurin's own package into /Library/Java/JavaVirtualMachines, so no symlink step is needed.
- Older posts use brew cask install java or the AdoptOpenJDK tap. brew cask commands were deprecated in Homebrew 2.6 in favour of brew install --cask, and AdoptOpenJDK moved to Eclipse Adoptium, whose builds are called Temurin.
brew install openjdk@21
sudo ln -sfn $(brew --prefix)/opt/openjdk@21/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-21.jdk
/usr/libexec/java_home -V
# or a Temurin package
brew install --cask temurin@21Switch Java versions with SDKMAN or jenv
SDKMAN installs JDKs from many vendors into ~/.sdkman/candidates/java and sets JAVA_HOME to the one in use. Install it with curl -s "https://get.sdkman.io" | zsh, and keep the lines it adds at the end of ~/.zshrc. sdk use switches the current shell, sdk default sets every new shell, and sdk env init writes a .sdkmanrc file that pins a version to a project folder (set sdkman_auto_env=true in ~/.sdkman/etc/config to switch on cd). Its JDKs live outside /Library/Java, so java_home -V doesn't list them.
jenv doesn't install Java. It switches between JDKs you already have: add each one with jenv add, then choose with jenv global, jenv local (writes a .java-version file in the folder) or jenv shell. Run jenv enable-plugin export once, or JAVA_HOME isn't set, and Gradle and Maven keep using the old Java.
Use one of them, and delete your own JAVA_HOME line from ~/.zshrc so it doesn't override the tool.
# SDKMAN
sdk list java
sdk install java 21.0.9-tem # pick an identifier from the list
sdk use java 21.0.9-tem # this shell
sdk default java 21.0.9-tem # every new shell
# jenv
brew install jenv
echo 'eval "$(jenv init -)"' >> ~/.zshrc && exec $SHELL -l
jenv add $(/usr/libexec/java_home -v 21)
jenv global 21
jenv enable-plugin exportAndroid Studio, IntelliJ, Gradle and Flutter pick their own JDK
- Android Studio runs Gradle with the JDK in Android Studio > Settings > Build, Execution, Deployment > Build Tools > Gradle > Gradle JDK. New projects use GRADLE_LOCAL_JAVA_HOME, which defaults to the JetBrains Runtime bundled with the IDE. ./gradlew in a terminal uses JAVA_HOME instead, which is why a build can pass in the IDE and fail in Terminal.
- IntelliJ IDEA: the project's JDK is under File > Project Structure > Project > SDK, where Download JDK also installs one. Gradle projects have their own Gradle JVM setting under Settings > Build, Execution, Deployment > Build Tools > Gradle.
- Gradle: org.gradle.java.home in gradle.properties picks the JDK for a project. Newer Gradle also supports Daemon JVM criteria (./gradlew updateDaemonJvm --jvm-version=17 writes gradle/gradle-daemon-jvm.properties), which take precedence over JAVA_HOME.
- Each Gradle version runs on Java up to a limit: Java 25 needs Gradle 9.1.0 or later, Java 26 needs 9.4.0, Java 27 needs 9.8.0. A JDK newer than the project's Gradle fails with errors like Unsupported class file major version 69 (69 is Java 25).
- Flutter picks its JDK in this order: the folder set with flutter config --jdk-dir, then the JDK bundled with Android Studio, then JAVA_HOME, then java on PATH, so changing JAVA_HOME alone often changes nothing for Flutter. flutter doctor -v shows the Java binary it picked. Point it at a version your apps' Gradle accepts with the command below, and set an empty value to go back to the default.
flutter config --jdk-dir="$(/usr/libexec/java_home -v 17)"
flutter doctor -v
# back to the default
flutter config --jdk-dir=""Doing it with Stackyard
Stackyard is a free, open-source app for Mac and Windows. It lists every JDK it finds, and its Get tools page installs any Java version straight from the publisher, checked against the publisher's hash. Before it installs, you choose the install folder and whether it sets JAVA_HOME and PATH. For Flutter apps it checks which Java each app's Gradle and Android Gradle Plugin accept, flags a Java that's too new, and offers Point Flutter at JDK N, which runs flutter config --jdk-dir. You see each step before it runs. Its doctor for a broken JAVA_HOME and dead PATH entries is Windows only.
Questions
How do I change the Java version from the Mac terminal?
- Run export JAVA_HOME=$(/usr/libexec/java_home -v 17) with the version you want. It lasts until you close that window. Put the same line in ~/.zshrc to make it the default.
Why does java -version still show the old version on my Mac?
- Another java comes first on your PATH, often a Homebrew openjdk bin folder, or a later line in ~/.zshrc or ~/.zprofile sets JAVA_HOME again. Run which -a java and grep JAVA_HOME ~/.zshrc ~/.zprofile, and open a new terminal after each change.
Where are JDKs installed on a Mac?
- Installer packages put them in /Library/Java/JavaVirtualMachines, for example jdk-25.jdk. Homebrew formulae live under /opt/homebrew on Apple silicon (/usr/local on Intel), SDKMAN uses ~/.sdkman/candidates/java, and Android Studio bundles its own in its jbr folder.
How do I remove an old JDK from my Mac?
- Delete its folder with sudo rm -rf /Library/Java/JavaVirtualMachines/jdk-17.jdk, using the name from java_home -V. For Homebrew, run brew uninstall openjdk@17 and delete the symlink you made. Don't touch /usr/bin/java, which belongs to macOS.
Which Java version should I use for Android and Flutter?
- Android Gradle Plugin 8 and 9 need Java 17 or newer, and your project's Gradle version caps how new it can be. Java 17 or 21 suits most current projects. If a build fails, check the Java in flutter doctor -v against Gradle's compatibility table.