To install Java on our system, We must first visit the official Oracle or OpenJDK website to download the Java Development Kit (JDK) installer. The JDK is required for Java development.
Here are the steps we can follow to Install Java on our machines.
The JDK contains everything we need to compile, debug, and run Java programs.
Download Java: https://openjdk.org
Navigate to the JDK download page.
Choose the appropriate version for your operating system (Windows, macOS, or Linux) and download the installer.
Once the download is complete, unzip the installer.
Installing the JDK involves running the installer and following the on-screen instructions.
Double-click the downloaded installer file (usually ending with .exe).
Follow the installation wizard instructions.
Specify the installation directory and other options as required and complete the installation process.
Double-click the downloaded disk image file (usually ending with .dmg).
Double-click the JDK package file to start the installation.
Follow the on-screen instructions provided by the installer and authenticate with your administrator password if prompted.
Open the ".zshrc" file in the root directory of the system.
Next, we need to set up environment variables such as JAVA_HOME and add the bin directory of the JDK to the system's PATH variable so other Java programs and tools can find the JDK. In the terminal, type the following command to create the Z-Shell resource file:
Press Control + D to save the file. The .zshrc file is run whenever a new terminal session is initialized, which sets "JAVA_HOME" and update "PATH" environment variables.
After installation, we can verify that Java is installed correctly by opening a command prompt (on Windows) or a terminal (on macOS or Linux).
This should display the installed Java version.
>java -version > >javac -version
# java version command output: openjdk version "21.0.2" 2024-01-16 OpenJDK Runtime Environment (build 21.0.2+13-58) OpenJDK 64-Bit Server VM (build 21.0.2+13-58, mixed mode, sharing) # javac version command output: javac 21.0.2
If we only need to run Java applications and not develop them, we can install the Java Runtime Environment (JRE) instead of the JDK.
The JRE includes everything you need to run Java applications but does not include development tools.