Lesson 5 of 50 – Java Installation
10%

Java Installation

Before writing and running Java programs, you need to install a Java Development Kit (JDK). The JDK provides the compiler, runtime tools, and other utilities required to develop Java applications.

Note: Install a suitable JDK before compiling Java programs. For beginners, a current long-term support (LTS) release is a good choice. You can download a JDK from Oracle or choose an OpenJDK distribution.
What Is JDK?

JDK stands for Java Development Kit. It is a collection of tools used to develop, compile, and run Java programs.

  • It includes the Java compiler.
  • It provides tools to run Java applications.
  • It includes development and diagnostic utilities.
  • It provides the Java APIs needed for development.

You need a JDK to compile Java source code using the javac command.

JDK vs JRE vs JVM

JDK, JRE, and JVM are related Java terms, but they have different meanings.

Term Full Form Purpose
JDK Java Development Kit Provides tools for developing Java programs.
JRE Java Runtime Environment Traditional term for the components needed to run Java applications.
JVM Java Virtual Machine Executes Java bytecode.
Remember: Modern JDK distributions commonly provide the runtime needed to run Java applications. A separate JRE installation is not always required.
System Requirements

Before installing Java, check that your computer supports the JDK distribution you plan to use.

  • A Windows, Linux, or macOS computer.
  • A compatible 64-bit or other supported system architecture.
  • Sufficient free disk space.
  • Administrator permission if required by the installer.
  • An internet connection to download the JDK.

Exact requirements depend on the JDK version and distribution you choose.

Download Java JDK

Download a JDK from an official vendor or an established OpenJDK distribution.

Official download options:

Basic steps:

  1. Open an official download website.
  2. Select the JDK version you want to install.
  3. Choose your operating system.
  4. Select the appropriate installer or archive.
  5. Download the file to your computer.
Install Java on Windows

Follow these steps to install a JDK on Windows using an installer package.

  1. Download the Windows installer for your selected JDK.
  2. Open the downloaded installer.
  3. Follow the installation wizard.
  4. Choose the installation location if prompted.
  5. Complete the installation.
  6. Open Command Prompt to verify the installation.
Example installation location:
C:\Program Files\Java\jdk-21

This is an example path only. The actual folder name depends on the JDK version and vendor.

Set the JAVA_HOME Environment Variable

JAVA_HOME is an environment variable commonly used by Java development tools to identify the JDK installation directory.

Example value:
C:\Program Files\Java\jdk-21

Steps on Windows:

  1. Search for Environment Variables in Windows.
  2. Open the system environment variable settings.
  3. Select Environment Variables.
  4. Create a variable named JAVA_HOME.
  5. Set its value to your actual JDK folder.
  6. Save the changes.

Use your actual installation path. Do not include the bin folder in the JAVA_HOME value.

Add Java to the PATH Variable

The PATH environment variable allows you to run Java commands from Command Prompt without typing the complete path to the executable.

Add the JDK's bin folder to PATH.

Example PATH entry:
%JAVA_HOME%\bin

Steps:

  1. Open Environment Variables.
  2. Select the Path variable.
  3. Choose Edit.
  4. Add a new entry: %JAVA_HOME%\bin.
  5. Save the changes.
  6. Open a new Command Prompt window.
If another Java version is earlier in PATH, the system may run that version instead. Check the reported version after changing PATH.
Verify Java Installation

After installing Java, verify that the Java executable is available.

Command 1:
java -version

This command displays information about the Java runtime being used.

Command 2:
javac -version

This command displays the version of the Java compiler.

Example Output:
java version "21..."
javac 21...

The exact output depends on your installed JDK. Both commands should work when the JDK is installed correctly and its tools are available through PATH.

Install Java on Linux

On Linux, Java can often be installed through the distribution's package manager or from an official JDK archive.

Example for Ubuntu or Debian:
sudo apt update
sudo apt install openjdk-21-jdk

The package must be available in your configured repositories. If it is unavailable, use a supported JDK package or installation method for your distribution.

Verify the Installation:
java -version
javac -version
Install Java on macOS

On macOS, you can install a compatible JDK using a vendor-provided installer or an appropriate package manager.

  1. Download the macOS JDK installer for your system.
  2. Open the downloaded package.
  3. Follow the installation instructions.
  4. Open Terminal.
  5. Verify the installation using Java commands.
Verification Commands:
java -version
javac -version

Ensure you choose a JDK build that supports your Mac's processor architecture.

Common Installation Problems

Beginners may encounter errors while installing Java or configuring environment variables.

Problem Possible Solution
java is not recognized Check PATH and open a new terminal.
javac is not recognized Install a JDK and check its bin directory.
Wrong Java version Check PATH order and the active Java installation.
Installation fails Verify system compatibility and installer requirements.
JAVA_HOME not working Set JAVA_HOME to the JDK root directory.
Useful diagnostic commands:
java -version
javac -version
where java
where javac

The where commands are for Windows. On Linux or macOS, use which java and which javac.

Compile and Run Your First Java Program

Once the JDK is installed, create a Java source file and compile it from your terminal.

Step 1: Create Main.java
public class Main {
    public static void main(String[] args) {
        System.out.println("Java is installed successfully!");
    }
}
Step 2: Compile
javac Main.java
Step 3: Run
java Main
Output:
Java is installed successfully!

Run these commands in the folder containing Main.java. The filename must match the public class name.

Key Points
  • JDK stands for Java Development Kit.
  • The JDK provides tools for developing Java programs.
  • JVM executes Java bytecode.
  • JAVA_HOME commonly identifies the JDK installation folder.
  • PATH allows Java commands to run from the terminal.
  • Use java -version to check the runtime version.
  • Use javac -version to check the compiler version.
  • Use javac Main.java to compile a basic Java program.
  • Use java Main to run the compiled program.
  • Installation steps vary by operating system and JDK distribution.


🧠 Quick Quiz

1. Which Java component is needed to compile Java source code?





2. Which command displays the Java compiler version?





3. Which command runs a compiled Main class?