Skip to main content

πŸ› οΈ Java Installation and Setup

Before you write your first Java program, you need to set up your machine to understand Java.


β˜• Fun Fact​

The tool you’ll install to run Java is called a JDK (Java Development Kit). Think of it as your Java toolbox with everything you need to code, compile, and run Java programs.


πŸ’» What You Need​

To run Java code on your system:

  • JDK (Java Development Kit) – for compiling and running Java.
  • Text Editor or IDE – e.g., VS Code, IntelliJ IDEA, Eclipse.

πŸ“₯ Installing Java JDK​

πŸ“Œ We’ll install OpenJDK 21 (latest LTS) for maximum compatibility.

πŸ”΅ Windows​

  1. Go to https://jdk.java.net/
  2. Download the Windows .zip or installer for OpenJDK 21.
  3. Extract (if zip) and place it in a known location (e.g., C:\Java\jdk-21)
  4. Set environment variables:
    • JAVA_HOME = C:\Java\jdk-21
    • Add %JAVA_HOME%\bin to Path

🍏 macOS​

  1. Use Homebrew:
brew install openjdk@21

Add this to your shell config (.zshrc or .bash_profile):

```bash
export JAVA_HOME=$(/usr/libexec/java_home -v21)
export PATH=$JAVA_HOME/bin:$PATH

🐧 Linux (Ubuntu/Debian)​

Run:

sudo apt update
sudo apt install openjdk-21-jdk

Verify installation:

java -version

πŸ§ͺ Verify Installation​

Open your terminal or command prompt and run:

java -version

You should see output like:

openjdk version "21.0.2" 2024-01-16

βœ… Congrats! Java is installed.​

πŸ§’ Explain Like I'm Five​

Imagine you're trying to speak Java, but your computer doesn't understand it yet. Installing the JDK is like giving your computer a Java dictionary and voice so it can talk Java with you!

✏️ Real-World Analogy​

Installing the JDK is like installing a kitchen before cooking. You need the tools (JDK), not just the recipe (Java code).

πŸ“ Exercise​

Check Java on Your System βœ… Run this command:

java -version

What version do you see? Write it here: _________

πŸ§‘πŸ»β€πŸ’» Extra Challenge​

Try writing a file Hello.java with this code:

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

Compile and run:

javac Hello.java
java Hello

βœ… What’s Next?​

Now that Java is ready, let’s write your first Java program!

πŸ‘‰ Next: Hello World in Java