Skip to content

Instantly share code, notes, and snippets.

@krutilin
Last active December 3, 2022 11:04
Show Gist options
  • Save krutilin/b32091a785b955e6bd9b6b0ea7bfcc57 to your computer and use it in GitHub Desktop.
Save krutilin/b32091a785b955e6bd9b6b0ea7bfcc57 to your computer and use it in GitHub Desktop.
How to set or change the default Java (JDK) version on OSX (Mac)?

First of all check current java versions, run following command in the terminal

/usr/libexec/java_home -V

which will output something like the following:

Matching Java Virtual Machines (2): 9, x86_64: "Java SE 9" /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home 1.8.0_152, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home

Choose the version you want to be the default (1.8.0_152 for example) then put following in to your .bash_profile:

export JAVA_HOME=/usr/libexec/java_home -v 1.8.0_152

Also you can specify just the major version, if it's unique, like:

export JAVA_HOME=/usr/libexec/java_home -v 1.8

Now when you check

java -version

you will got following output:

java version "1.8.0_152" Java(TM) SE Runtime Environment (build 1.8.0_152-b16) Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)

@Crachman
Copy link

but if you close the terminal, and restart the terminal, it's not work.

@Rakagustipradana
Copy link

Rakagustipradana commented Dec 3, 2022

not work from my side

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