How to download, install & set $JAVA_HOME on macOS
This article shows you how to download, install and set the $JAVA_HOME on macOS.
Steps to set the $JAVA_HOME environment variable on macOS.
- Find out your macOS version.
- Find out which shell you are using, bash or zsh?
- For zsh shell, export $JAVA_HOME at .zshenv or .zprofile or .zshrc
- For bash shell, export $JAVA_HOME at .bash_profile or .bashrc
- Source with source .zshenv
- Test with echo $JAVA_HOME
- Done.
Find out your macOS version
- Click on the icon in your Mac.
- Click on 'About this Mac'.
Find out which cell you are using, bash or zsh?
- For bash shell, we can put the environment variables at .bash_profile or .bashrc
- For zsh shell, we can put the environment variables at .zshenv or .zprofile or .zshrc
- To know, your default Terminal shell open the Terminal on Mac.
- Type echo $SHELL
- Press return key.
- For example,
- echo $SHELL
- /bin/zsh
What is /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home
Also, find all installed JDKs.
Matching Java Virtual Machines (2):
17.0.1 (x86_64) "Oracle Corporation" - "Java SE 17.0.1" /Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home
1.8.0_321 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home(Default JDK)
Also, run a specified JDK command.
Set $JAVA_HOME
Open the .zshenv
Add the following content
Source the file and print the $JAVA_HOME, done.
/Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home
Switch between different JDK versions
Matching Java Virtual Machines (2):
17.0.1 (x86_64) "Oracle Corporation" - "Java SE 17.0.1" /Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home
1.8.0_321 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home(Default JDK)
For zsh shell, edit the .zshenv
/usr/libexec/java_home -v "${Version}" to activate a specified JDK version.
Add the following content to activate the JDK 1.8
Source the file and print the $JAVA_HOME, done.
/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home
Comments
Post a Comment