Java
Module 1: Java Introduction & Environment SetupBeginner18 min read

Java Installation, PATH Setup & Command-Line Execution

Install JDK on Windows, Linux, and macOS, configure JAVA_HOME and PATH, set up IntelliJ IDEA / Eclipse / VS Code, and pass command-line arguments.

Key Takeaways & Core Concepts
  • Windows (PowerShell / System Environment Variables): * Set JAVA_HOME = C:\Program Files\Java\jdk-21
  • Linux / macOS (`~/.bashrc` or `~/.zshrc`): ```bash
  • IntelliJ IDEA: Industry-standard Java IDE with deep refactoring, Maven/Gradle integration, and visual debugger.
  • VS Code: Lightweight editor with Microsoft's "Extension Pack for Java".

Introduction

A reliable Java development setup requires installing an LTS JDK (Java 17 or Java 21), configuring JAVA_HOME and PATH, and knowing how to compile, package, and run programs from both the terminal and an IDE.

Handling Command-Line Arguments Safely
1public class Main {
2 public static void main(String[] args) {
3 if (args.length == 0) {
4 System.out.println("No command-line arguments passed. Using default: Guest");
5 } else {
6 System.out.println("Arguments count: " + args.length);
7 for (int i = 0; i < args.length; i++) {
8 System.out.println("arg[" + i + "] = " + args[i]);
9 }
10 }
11 }
12}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
12 lines
Note: Always check `args.length` before accessing `args[i]` to prevent `ArrayIndexOutOfBoundsException`.

Learning Objectives

  • Install an OpenJDK / Oracle JDK on Windows, Linux, or macOS.
  • Configure JAVA_HOME and PATH environment variables accurately.
  • Compile and run Java programs from the CLI, including passing String[] args.

Setting Environment Variables

  • Windows (PowerShell / System Environment Variables):
  • Set JAVA_HOME = C:\Program Files\Java\jdk-21
  • Add %JAVA_HOME%\bin to Path.
  • Linux / macOS (`~/.bashrc` or `~/.zshrc`):
BASH Code Example
1export JAVA_HOME=/usr/lib/jvm/java-21-openjdk
2export PATH="$JAVA_HOME/bin:$PATH"
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
2 lines

Verify your setup by running java -version and javac -version.

Command-Line Execution & Arguments

Inside public static void main(String[] args), args[0] is the first CLI argument passed after the class name. Since Java 11, you can also run a single source file directly with java Main.java without a separate javac step.

Recommended IDEs

  • IntelliJ IDEA: Industry-standard Java IDE with deep refactoring, Maven/Gradle integration, and visual debugger.
  • VS Code: Lightweight editor with Microsoft's "Extension Pack for Java".
  • Eclipse: Mature open-source IDE widely used in enterprise teams.

Common Mistakes

  • Pointing JAVA_HOME to the bin folder instead of the JDK root directory—build tools like Maven and Gradle expect JAVA_HOME to be the parent of bin.
  • Forgetting to restart your terminal after editing environment variables.
Practice & AssessmentJava Programming Tutorial

Test Your Understanding

Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.

Your Current Score

0 / 0 correct(0 attempted)