Java Installation and Setup Guide

Introduction

Java is a versatile, platform-independent programming language used for developing applications ranging from desktop software and mobile apps to enterprise systems and web services. Before you can write and run Java programs, you need to install the Java Development Kit (JDK), which includes the Java Runtime Environment (JRE), compiler (javac), and essential development tools. This guide provides step-by-step instructions for installing and configuring Java on Windows, macOS, and Linux, along with verification steps and environment setup.


1. Understanding Java Editions

  • JDK (Java Development Kit): Required for developing Java applications. Includes compiler, debugger, and JRE.
  • JRE (Java Runtime Environment): Required only for running Java applications (not for development).
  • OpenJDK vs. Oracle JDK:
  • OpenJDK: Open-source, free, and the reference implementation (recommended for most users).
  • Oracle JDK: Commercially licensed (free for development, paid for production).

Recommendation: Use OpenJDK (e.g., from Adoptium, Amazon Corretto, or Azul Zulu).


2. Installing Java on Windows

Step 1: Download OpenJDK

  1. Go to Adoptium.net
  2. Select:
  • Version: LTS version (e.g., JDK 17 or JDK 21)
  • Operating System: Windows
  • Architecture: x64 (or ARM64 if applicable)
  • Package Type: .msi (installer)
  1. Click Download

Step 2: Run the Installer

  1. Double-click the downloaded .msi file.
  2. Follow the installation wizard (accept defaults).
  3. Note the installation path (e.g., C:\Program Files\Eclipse Adoptium\jdk-21.0.2.13-hotspot).

Step 3: Set Environment Variables

  1. Open Start Menu → Search for “Environment Variables” → Edit the system environment variables.
  2. Click Environment Variables…
  3. Under System Variables, find PathEditNew.
  4. Add the path to the bin directory:
   C:\Program Files\Eclipse Adoptium\jdk-21.0.2.13-hotspot\bin
  1. Click OK to save.

Step 4: Verify Installation

Open Command Prompt and run:

java -version
javac -version

✅ You should see version information for both.


3. Installing Java on macOS

Option A: Using Homebrew (Recommended)

  1. Install Homebrew (if not installed):
   /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  1. Install OpenJDK:
   brew install openjdk@21    # or openjdk@17
  1. Link the JDK (required for java command):
   sudo ln -sfn /opt/homebrew/opt/openjdk@21/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-21.jdk

Option B: Manual Installation

  1. Download .pkg from Adoptium.net
  2. Open the .pkg file and follow the installer.
  3. No environment setup needed—macOS auto-configures java and javac.

Verify Installation

Open Terminal and run:

java -version
javac -version

Note: On Apple Silicon Macs, ensure you download the ARM64 version.


4. Installing Java on Linux (Ubuntu/Debian)

Step 1: Update Package Index

sudo apt update

Step 2: Install OpenJDK

For JDK 21:

sudo apt install openjdk-21-jdk

For JDK 17 (LTS):

sudo apt install openjdk-17-jdk

Step 3: Set Default Java Version (if multiple versions installed)

sudo update-alternatives --config java
sudo update-alternatives --config javac

Select the desired version from the list.

Step 4: Set JAVA_HOME (Optional but Recommended)

  1. Find Java installation path:
   sudo update-alternatives --config java
# Example output: /usr/lib/jvm/java-21-openjdk-amd64/bin/java
  1. Edit shell profile (~/.bashrc, ~/.zshrc, or ~/.profile):
   export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
  1. Reload the profile:
   source ~/.bashrc

Verify Installation

java -version
javac -version
echo $JAVA_HOME

5. Verifying Your Installation

After installation, confirm everything works:

A. Check Java and Compiler Versions

java -version    # Should show runtime version
javac -version   # Should show compiler version

B. Write a Test Program

  1. Create HelloWorld.java:
   public class HelloWorld {
public static void main(String[] args) {
System.out.println("Java is working!");
}
}
  1. Compile and run:
   javac HelloWorld.java
java HelloWorld

✅ Output: Java is working!


6. Setting Up JAVA_HOME

Many tools (e.g., Maven, Gradle, Tomcat) require the JAVA_HOME environment variable.

Windows

  1. In Environment Variables, add a new System Variable:
  • Name: JAVA_HOME
  • Value: C:\Program Files\Eclipse Adoptium\jdk-21.0.2.13-hotspot

macOS / Linux

Add to your shell profile (~/.bashrc, ~/.zshrc):

export JAVA_HOME=/path/to/your/jdk

Find the path with:

  • macOS: /usr/libexec/java_home -v 21
  • Linux: readlink -f $(which java) | sed "s:bin/java::"

7. Managing Multiple Java Versions

If you work on projects requiring different Java versions, use a version manager:

  • Windows: Jabba or manual PATH switching
  • macOS/Linux: SDKMAN!
  curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install java 21-tem
sdk install java 17-tem
sdk use java 17-tem  # Switch version

8. Common Issues and Solutions

IssueSolution
'java' is not recognizedAdd JDK bin to PATH
JAVA_HOME not setSet JAVA_HOME to JDK root (not bin)
Permission denied (Linux)Use sudo for system-wide installs
Conflicting Java versionsUse update-alternatives (Linux) or SDKMAN!
macOS security warningGo to System Settings → Privacy & Security and allow the app

9. Recommended Development Tools

After installing Java, consider these tools:


Conclusion

You now have a fully functional Java development environment! By installing the JDK and configuring your system correctly, you’re ready to write, compile, and run Java applications. Remember to:

  • Use OpenJDK for a free, open-source solution.
  • Set JAVA_HOME for compatibility with build tools.
  • Keep your JDK updated for security and performance.

Whether you’re learning Java, building a personal project, or developing enterprise software, this setup provides a solid foundation. Happy coding! 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *


Macro Nepal Helper