In this post you will learn how to set the default JAVA_HOME
in Mac OS X when you have more than one JDK installed in your computer. First you need to run /usr/libexec/java_home -V
command to get the list of installed JDK. The command will print out something like the following depending on the available JDK in your computer.
On my machine I have the following version of Java.
Matching Java Virtual Machines (3):
9, x86_64: "Java SE 9" /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
1.8.0_121, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home
1.7.0_80, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
From the list above pick which version you want to be the default JDK. For example, I will choose the 1.8.0_121
version to be my default JDK. To set it run the command below.
export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_121`
If the major version of the available JDK is unique you can just use the major version, like:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
After setting the JAVA_HOME and you run the java -version
command you will see that JDK 1.8 is the new default JDK in your computer.
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
The change above will only be active in the current running shell. If you close or terminate the shell, next time you open the shell you will need to set it again. To make this change permanent you need to set it in your shell init file. For example if you are using bash
then you can set the command in the .bash_profile
. Add the following lines at the end of the file.
# Setting default JDK to version 1.8.
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
To activate this configuration right away your can run source .bash_profile
. This command reads and executes the .bash_profile
in the current shell.
- How do I use the LongToDoubleFunction functional interface in Java? - March 15, 2025
- How do I use the LongSupplier functional interface in Java? - March 14, 2025
- How do I use the LongPredicate functional interface in Java? - March 14, 2025
Thank you for the clear information. It helped me solve a problem I had since yesterday.
after export JAVA_HOME=
/usr/libexec/java_home -v 1.8.0_121
got bellow error
java --version
Hi Krishna,
Use single dash (
-version
) for the version options.I’m not sure if it’s worked. When I write
java -version
command it shows me the desired version 1.8. But when I write/usr/libexec/java_home
command it shows/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
. I’m confused is 1.8 my default version or not?Hi Sumaia,
When you get the correct version when running the
java -version
it means your setting is correct. While thejava_home
command when executed will give you an example path for setting theJAVA_HOME
variable. In your case it return JDK 11.From the manual the
java_home
command:The
java_home
command returns a path suitable for setting the JAVA_HOME environment variable. It determines this path from the user’s enabled and preferred JVMs in the Java Preferences application. Additional constraints may be provided to filter the list of JVMs available. By default, if no constraints match the available list of JVMs, the default order is used. The path is printed to standard output.If you’re using Mac OS Catalina, it may block graalvm from executing.
Do ensure you change the user and group after copying the graalvm directory to
/Library/Java/JavaVirtualMachines/
as follows:This was a really helpful article. Thank you!
Very helpful! Thanks!
How to make this default, since the .bash profile is completely locked? Save a duplicate?
Hi Martin,
What do you mean by the
.bash_profile
is completely locked? You can always edit the file and make changes to it.Thanks a bunch.. It solved my problem too..
This didn’t solve my issue. I am using maven which need Java 19, and predefined version is 18. When I switch through commands to 11, after restarting terminal 18 is back. I want Java 11 and delete 18 even it is dependency for maven.
Hi Kudo,
You need to export
JAVA_HOME
on your.bash_profile
. If not, it will be gone when you close the terminal.