Java Core: Introduction to Java - JDK & JRE Setup
This document outlines the setup process for the Java Development Kit (JDK) and Java Runtime Environment (JRE). Understanding the difference between these is crucial for Java development.
What are JDK and JRE?
JRE (Java Runtime Environment): Provides the minimum requirements to run Java applications. It includes the Java Virtual Machine (JVM), core class libraries, and other supporting files. If you only want to use Java applications, you only need the JRE.
JDK (Java Development Kit): A superset of the JRE. It includes everything in the JRE plus development tools needed to create Java applications. This includes the compiler (javac), debugger, and other utilities. If you want to write Java code, you need the JDK.
Think of it this way:
- JRE: Like a DVD player - it can play pre-made DVDs (Java applications).
- JDK: Like a DVD player and a DVD authoring studio - it can play DVDs and create them.
1. Downloading the JDK
Choose a Vendor: Several vendors provide JDK distributions. The most common are:
- Oracle JDK: The official JDK from Oracle. Requires an Oracle account for some versions. (https://www.oracle.com/java/technologies/downloads/)
- OpenJDK: An open-source implementation of Java. Often preferred for its licensing. (https://openjdk.java.net/)
- Adoptium (Eclipse Temurin): A popular OpenJDK distribution, known for its reliability and long-term support. (https://adoptium.net/) Recommended for beginners.
Download the Correct Version: Select the appropriate JDK version for your operating system (Windows, macOS, Linux) and architecture (x64, x86, ARM). Consider the following:
- LTS (Long-Term Support) Versions: These versions receive updates and security patches for an extended period. Currently, LTS versions are Java 8, Java 11, Java 17, and Java 21. Java 17 or 21 are generally recommended for new projects.
- Latest Version: Provides the newest features but may have less stability.
Download the Installer: Download the installer file (e.g.,
.exefor Windows,.dmgfor macOS,.tar.gzfor Linux).
2. Installing the JDK
The installation process varies slightly depending on your operating system. Here are instructions for common platforms:
A. Windows:
- Run the Installer: Double-click the downloaded
.exefile. - Follow the Wizard: Accept the license agreement and follow the on-screen instructions.
- Important: During installation, the installer will ask for a destination directory. Note this directory! You'll need it later to set the
JAVA_HOMEenvironment variable. The default is usuallyC:\Program Files\Java\jdk-<version>. - Complete Installation: Finish the installation process.
B. macOS:
- Open the DMG File: Double-click the downloaded
.dmgfile. - Run the Installer: Double-click the package file within the DMG.
- Follow the Wizard: Accept the license agreement and follow the on-screen instructions.
- Important: The JDK is typically installed in
/Library/Java/JavaVirtualMachines/jdk-<version>.jdk/Contents/Home. Note this directory! - Complete Installation: Finish the installation process.
C. Linux (Debian/Ubuntu - using tar.gz):
- Extract the Archive: Open a terminal and navigate to the directory where you downloaded the
tar.gzfile. Run the following command:tar -xvf jdk-<version>_linux-x64_bin.tar.gz - Move the JDK: Move the extracted directory to a suitable location, such as
/usr/lib/jvm:sudo mv jdk-<version> /usr/lib/jvm - Important: Note the full path to the JDK directory (e.g.,
/usr/lib/jvm/jdk-<version>).
3. Setting the JAVA_HOME Environment Variable
The JAVA_HOME environment variable tells your system where the JDK is installed. This is essential for many Java tools and IDEs to function correctly.
A. Windows:
- Search for "Environment Variables": Type "environment variables" in the Windows search bar and select "Edit the system environment variables."
- System Properties: Click the "Environment Variables..." button.
- System Variables: In the "System variables" section, click "New..."
- Variable Name: Enter
JAVA_HOME. - Variable Value: Enter the path to your JDK installation directory (e.g.,
C:\Program Files\Java\jdk-17). - Edit
PathVariable: Select thePathvariable in the "System variables" section and click "Edit..." - Add New Entry: Click "New" and add
%JAVA_HOME%\bin. - OK: Click "OK" on all open windows to save the changes.
B. macOS/Linux:
Open Terminal: Open a terminal window.
Edit Shell Configuration File: Edit your shell configuration file (e.g.,
.bashrc,.zshrc,.profile). The file you need to edit depends on the shell you're using. For example, if you're using Bash, edit.bashrc.Add the Following Lines: Add the following lines to the end of the file, replacing
<JDK_PATH>with the actual path to your JDK installation directory:export JAVA_HOME=<JDK_PATH> export PATH=$JAVA_HOME/bin:$PATHExample (using Java 17 on macOS):
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home export PATH=$JAVA_HOME/bin:$PATHSource the Configuration File: Run the following command to apply the changes:
source ~/.bashrc # Or source ~/.zshrc, source ~/.profile, etc.
4. Verifying the Installation
Open a New Terminal/Command Prompt: It's important to open a new terminal or command prompt window to ensure the environment variables are loaded.
Run the Following Command:
java -version javac -versionCheck the Output: If the installation was successful, you should see output similar to this:
java version "17.0.2" 2022-01-18 Java(TM) SE Runtime Environment (build 17.0.2+8-LTS) Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-LTS, mixed mode, sharing) javac 17.0.2This confirms that both the JRE (java) and the JDK (javac) are installed and accessible.
Troubleshooting
javaorjavacnot recognized: Double-check that theJAVA_HOMEenvironment variable is set correctly and that the%JAVA_HOME%\bin(Windows) or$JAVA_HOME/bin(macOS/Linux) directory is in yourPATH.- Incorrect Java Version: If you have multiple JDKs installed, ensure that the
JAVA_HOMEvariable points to the desired version. - Permissions Issues (Linux): Make sure you have the necessary permissions to execute the JDK files.
This guide provides a comprehensive overview of setting up the JDK and JRE. Remember to choose the appropriate version for your needs and follow the instructions carefully for your operating system. Good luck!