Skip to main content
Java Basics
CHAPTER 02 Beginner

Installing Java and Setting Up Environment

Updated: May 17, 2026
5 min read

# CHAPTER 2

Installing Java and Setting Up Environment

1. Introduction

Before you can write a single line of Java, you need the right tools installed on your computer. Think of it like cooking: you need a kitchen (JDK), utensils (IDE), and a recipe book (documentation) before you can make your first dish. In this chapter, we'll install everything from scratch and verify it works.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Download and install the Java Development Kit (JDK).
  • Configure the JAVA_HOME environment variable.
  • Install and configure IntelliJ IDEA or Eclipse.
  • Compile and run a Java program from the command line.
  • Understand the compilation and execution process.

3. Step 1: Downloading the JDK

Visit the official Oracle website or use the open-source OpenJDK:
  • Oracle JDK: https://www.oracle.com/java/technologies/downloads/
  • OpenJDK (Adoptium/Temurin): https://adoptium.net/

Download JDK 21 LTS (Long Term Support) for your operating system.

4. Step 2: Installing the JDK

Windows:

  1. 1. Run the .msi installer.
  1. 2. Accept the license and click "Next" through the wizard.
  1. 3. Note the installation path (default: C:\Program Files\Java\jdk-21).

macOS:

  1. 1. Run the .dmg installer.
  1. 2. Follow the on-screen instructions.

Linux (Ubuntu/Debian):

bash
12
sudo apt update
sudo apt install openjdk-21-jdk

5. Step 3: Setting JAVAHOME

Why? Many Java tools (Maven, Gradle, Tomcat) look for the JAVAHOME variable to find the JDK.

Windows:

  1. 1. Open System Properties → Advanced → Environment Variables.
  1. 2. Click "New" under System Variables.
  1. 3. Variable name: JAVAHOME
  1. 4. Variable value: C:\Program Files\Java\jdk-21
  1. 5. Add %JAVAHOME%\bin to the Path variable.

macOS/Linux:

bash
123
# Add to ~/.bashrc or ~/.zshrc
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH

6. Step 4: Verifying Installation

Open your terminal or command prompt and type:
bash
1
java -version

Expected Output:

123
java version "21.0.2" 2024-01-16 LTS
Java(TM) SE Runtime Environment (build 21.0.2+13-LTS-58)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.2+13-LTS-58, mixed mode, sharing)

Also verify the compiler:

bash
1
javac -version

Expected Output:

1
javac 21.0.2

If both commands work, Java is installed correctly!

7. Step 5: Installing an IDE

Option A: IntelliJ IDEA (Recommended)

  • Download from: https://www.jetbrains.com/idea/
  • Community Edition is free and perfect for learning.
  • Features: Intelligent code completion, refactoring, debugger, and built-in terminal.

Option B: Eclipse

  • Download from: https://www.eclipse.org/downloads/
  • Free and open-source.
  • Well-suited for enterprise Java development.

Option C: VS Code

  • Install the "Extension Pack for Java" by Microsoft.
  • Lightweight and fast for small projects.

8. Step 6: Compiling and Running from Command Line

The Java compilation process has two stages:

1234
                    javac               java
Source Code  ───────────────>  Bytecode  ─────────────>  Output
Hello.java                    Hello.class               "Hello!"
(Human readable)              (JVM readable)            (Result)

Step 1: Create a file called Hello.java:

java
12345
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, Java World!");
    }
}

Step 2: Compile with javac:

bash
1
javac Hello.java

This creates a file called Hello.class (the bytecode).

Step 3: Run with java:

bash
1
java Hello

Output:

1
Hello, Java World!

Important: The filename must match the public class name. Hello.java must contain public class Hello.

9. Understanding the Compilation Process

1234567891011121314
+------------------+        +------------------+        +------------------+
|   Hello.java     |  javac |   Hello.class    |  java  |                  |
|   (Source Code)   | -----> |   (Bytecode)     | -----> | Output on Screen |
|   Human-readable |        |   JVM-readable   |        |                  |
+------------------+        +------------------+        +------------------+
                                     |
                                     v
                            +------------------+
                            |       JVM        |
                            | (Interprets the  |
                            |  bytecode for    |
                            |  your specific   |
                            |  OS: Win/Mac/Lin)|
                            +------------------+

10. Mini Project: Personal Info Printer

java
123456789101112
public class PersonalInfo {
    public static void main(String[] args) {
        System.out.println("===========================");
        System.out.println("    MY PERSONAL INFO");
        System.out.println("===========================");
        System.out.println("Name: John Doe");
        System.out.println("Age: 22");
        System.out.println("Language: Java");
        System.out.println("Goal: Become a Software Engineer");
        System.out.println("===========================");
    }
}

11. Common Mistakes

  • Not matching filename to class name: MyApp.java must contain public class MyApp. Mismatches cause compilation errors.
  • Forgetting to set PATH: If java -version says "command not found," the PATH variable is not configured correctly.
  • Using a text editor without saving as .java: Saving as .txt will fail compilation.

12. Best Practices

  • Always use the latest LTS version of Java (17 or 21).
  • Use IntelliJ IDEA for the best developer experience.
  • Learn to compile from the command line first before relying on an IDE — it builds fundamental understanding.

13. Exercises

  1. 1. Install JDK 21 on your machine and verify it with java -version.
  1. 2. Write a program that prints your full name, city, and favorite programming language.
  1. 3. Compile and run it from the command line using javac and java.

14. MCQ Quiz with Answers

Question 1

Which tool is used to compile Java source code?

Question 2

What file extension does a Java source file have?

Question 3

What file is generated after compilation?

Question 4

What environment variable points to the JDK installation?

Question 5

Which IDE is developed by JetBrains for Java?

Question 6

What is the correct command to run a compiled Java class called Hello?

Question 7

The .class file contains what type of code?

Question 8

Which command verifies your Java installation?

Question 9

What happens if your filename doesn't match the public class name?

Question 10

What does the JIT compiler do?

15. Interview Questions

  • Q: Explain the Java compilation and execution process step by step.
  • Q: What is the difference between javac and java commands?
  • Q: Why does the filename in Java need to match the public class name?
  • Q: What is Just-In-Time (JIT) compilation?

16. FAQs

Q: Do I need to pay for Java? A: No. OpenJDK is completely free and open-source. Oracle JDK is free for development but requires a license for commercial production use in some versions.

Q: Can I use VS Code for Java? A: Yes, with the "Extension Pack for Java" installed, VS Code becomes a capable Java development environment.

17. Summary

Setting up your Java development environment involves installing the JDK, configuring JAVA_HOME, and choosing an IDE. The Java compilation process is two-step: javac compiles .java files into .class bytecode, and java runs that bytecode on the JVM. With your environment ready, you're now prepared to write real Java code.

18. Next Chapter Recommendation

Your environment is set up and running. In Chapter 3: Java Syntax and First Program, we'll dive deep into the anatomy of a Java program — understanding every keyword, the main method, comments, and building your first real calculator.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·