Change the default Java Version on macOS

Here, We are trying to change the version to Java SE 8

First run /usr/libexec/java_home -V which will output something like the following:

Matching Java Virtual Machines (3):
16.0.2 (x86_64) “Oracle Corporation” – “Java SE 16.0.2” /Library/Java…


This content originally appeared on DEV Community and was authored by Rithvik

Here, We are trying to change the version to Java SE 8

First run /usr/libexec/java_home -V which will output something like the following:

Matching Java Virtual Machines (3):
    16.0.2 (x86_64) "Oracle Corporation" - "Java SE 16.0.2" /Library/Java/JavaVirtualMachines/jdk-16.0.2.jdk/Contents/Home
    1.8.202.08 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
    1.8.0_202 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-16.0.2.jdk/Contents/Home

Pick the version you want to be the default (1.8.0_202) then:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_202`

Now when you run java -version you will see:

java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)

Add the

export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_202`

line to your shell’s init file.

Updating the .zshrc file should work:

nano ~/.zshrc

paste the line

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0_202)

at bottom of the file.

Press CTRL+X to exit the editor, Press Y to save your changes.

source ~/.zshrc
echo $JAVA_HOME
java -version

Output should be:

java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)

Resources

Stackoverflow


This content originally appeared on DEV Community and was authored by Rithvik


Print Share Comment Cite Upload Translate Updates
APA

Rithvik | Sciencx (2022-02-19T06:02:49+00:00) Change the default Java Version on macOS. Retrieved from https://www.scien.cx/2022/02/19/change-the-default-java-version-on-macos/

MLA
" » Change the default Java Version on macOS." Rithvik | Sciencx - Saturday February 19, 2022, https://www.scien.cx/2022/02/19/change-the-default-java-version-on-macos/
HARVARD
Rithvik | Sciencx Saturday February 19, 2022 » Change the default Java Version on macOS., viewed ,<https://www.scien.cx/2022/02/19/change-the-default-java-version-on-macos/>
VANCOUVER
Rithvik | Sciencx - » Change the default Java Version on macOS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/19/change-the-default-java-version-on-macos/
CHICAGO
" » Change the default Java Version on macOS." Rithvik | Sciencx - Accessed . https://www.scien.cx/2022/02/19/change-the-default-java-version-on-macos/
IEEE
" » Change the default Java Version on macOS." Rithvik | Sciencx [Online]. Available: https://www.scien.cx/2022/02/19/change-the-default-java-version-on-macos/. [Accessed: ]
rf:citation
» Change the default Java Version on macOS | Rithvik | Sciencx | https://www.scien.cx/2022/02/19/change-the-default-java-version-on-macos/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.