Download JDK: Visit the official Oracle website or the OpenJDK website to download the JDK installer for macOS. Ensure you download the version that matches your macOS version and architecture (32-bit or 64-bit).
Run Installer: Once the download is complete, locate the downloaded file (it's typically a .dmg file) and double-click it to mount the disk image.
Run Package Installer: Inside the mounted disk image, you'll find a package file with a .pkg extension. Double-click this file to run the package installer.
Follow Installation Instructions: Follow the instructions provided by the installer. Typically, you will need to click through a few screens and agree to the license terms.
Authenticate: During the installation process, macOS may require you to enter your administrator password to proceed.
Installation Complete: After the installation is complete, you can verify that the JDK has been installed by opening a terminal window and typing the following command:
java -version
This command will display the installed version of Java. If you see version information, then the JDK has been successfully installed.
Set JAVA_HOME (Optional): It's often helpful to set the JAVA_HOME environment variable to point to the JDK installation directory. To do this, open your terminal and run the following command:
bashexport JAVA_HOME=$(/usr/libexec/java_home)
You may want to add this line to your shell configuration file (e.g., ~/.bash_profile, ~/.bashrc, or ~/.zshrc) to make the change persistent across terminal sessions.
That's it! You've now successfully installed the JDK on your Mac. You can start developing Java applications using your preferred IDE or text editor.