About Us Contact Us Write for Us Advertise
Home > Java > How to Install Java and Set Up Your First Program (Step by Step)
Java

How to Install Java and Set Up Your First Program (Step by Step)

A beginner's step-by-step guide to installing the Java JDK on Windows, Mac, or Linux, choosing an IDE, and writing and running your very first Java program — with a no-install option too.

Shiv Pandey
Shiv Pandey
Sep 16, 2026 | 89 views
How to Install Java and Set Up Your First Program (Step by Step)

In Lesson 1 you learned what Java is and how it works. Now it's time to get your hands dirty and actually run some Java on your own computer. Don't worry — this is easier than it sounds, and by the end of this lesson you'll have written and run your very first Java program. 🎉

We'll set up everything step by step, and I'll also show you a zero-installation option in case you just want to start coding in the next two minutes.

What you need to run Java

To write and run Java programs, you need two things:

  • The JDK (Java Development Kit) — this includes the compiler (javac) and the runtime (JVM) needed to build and run Java. Installing the JDK is really the only essential step.
  • A place to write code — this can be a simple text editor, a full IDE (recommended), or even an online compiler.

The fastest way to start (no installation)

If you just want to try Java right now without installing anything, use an online Java compiler like Programiz, OnlineGDB, or Replit. Open one in your browser, paste in a program, and hit Run. It's perfect for these early lessons. When you're ready to build real projects, come back and install the JDK properly below.

Step 1: Install the JDK

Download the JDK from a trusted source — Oracle JDK or the free, open-source OpenJDK (Adoptium/Temurin is a popular, easy choice). Pick the LTS (Long-Term Support) version, such as Java 17 or 21 — LTS versions are stable and supported for years.

  • Windows: download the .msi installer and run it (Next → Next → Finish).
  • macOS: download the .pkg installer and run it.
  • Linux: use your package manager, e.g. sudo apt install openjdk-17-jdk.

Step 2: Verify the installation

Open a terminal (Command Prompt on Windows, Terminal on Mac/Linux) and type:

java -version
javac -version

If you see version numbers (like openjdk version "17.0.10"), you're all set! If instead you get "command not found," the JDK isn't on your system PATH — the installer usually handles this, but if not, a quick search for "add Java to PATH on [your OS]" will fix it.

Step 3: Choose where to write code

You have three good options:

  • IntelliJ IDEA (Community Edition) — the most popular Java IDE; smart, beginner-friendly, and free. Recommended.
  • VS Code — lightweight; add the "Extension Pack for Java" and you're ready.
  • Eclipse — a long-standing free Java IDE.

An IDE gives you autocomplete, error highlighting, and a one-click Run button — a huge help when you're learning. If you're just starting, install IntelliJ IDEA Community Edition.

Step 4: Write and run your first program

Create a file named Main.java (the file name must match the class name) and type this in:

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, GyaanPost! I just ran my first Java program.");
    }
}

To run it from the terminal (if you're not using an IDE's Run button):

# 1) Compile: turns Main.java into Main.class (bytecode)
javac Main.java

# 2) Run: the JVM executes the bytecode
java Main

You should see:

Hello, GyaanPost! I just ran my first Java program.

Notice the two steps — compile then run — exactly the flow we drew in Lesson 1: your .java source becomes .class bytecode, which the JVM then runs. (In an IDE, the Run button does both for you automatically.)

Troubleshooting common first errors

  • "javac is not recognized" — the JDK isn't on your PATH. Reinstall or add it to PATH.
  • "class Main is public, should be declared in a file named Main.java" — your file name must exactly match the public class name (including capital M).
  • Nothing prints — check you're running java Main (no .class) from the same folder.

I still remember how satisfying it felt the first time "Hello" actually printed on my screen — that tiny moment is where every developer's journey really begins. If yours just printed, congratulations: you're officially writing Java. 🚀

Key takeaways

  • Install the JDK (LTS version like 17 or 21) — it's the one essential piece.
  • Verify with java -version and javac -version.
  • Use an IDE like IntelliJ IDEA Community Edition (or an online compiler to start instantly).
  • Compile with javac Main.java, then run with java Main — compile then run.

← Previous: Lesson 1 — What is Java?
Next: Lesson 3 — Java Syntax Basics: Your First Program Explained →

Frequently Asked Questions

What do I need to install to run Java?

You need the JDK (Java Development Kit), which includes the compiler (javac) and the runtime (JVM). Installing the JDK is the only essential step. To write code you can use an IDE like IntelliJ IDEA, a text editor, or even an online compiler.

Which Java version should a beginner install?

Install a Long-Term Support (LTS) version such as Java 17 or Java 21. LTS versions are stable and supported for years. You can use free OpenJDK builds like Adoptium Temurin, or Oracle JDK.

How do I run a Java program?

Save your code in a file named Main.java, compile it with the command javac Main.java (which creates Main.class bytecode), then run it with java Main. In an IDE, the Run button does both steps for you automatically.

Can I run Java without installing anything?

Yes. Online Java compilers like Programiz, OnlineGDB, or Replit let you write and run Java in your browser with no installation, which is great for getting started quickly. Install the JDK when you are ready to build real projects.

Related Articles

Java Syntax Basics: Your First Program Explained Line by Line
Java

Java Syntax Basics: Your First Program Explained Line by Line

Build a Java Console Project: A To-Do App (Step by Step)
Java

Build a Java Console Project: A To-Do App (Step by Step)

Build Your First Spring Boot App (Step-by-Step Beginner Guide)
Java

Build Your First Spring Boot App (Step-by-Step Beginner Guide)

List, Set and Map in Java: How to Use Each (with Examples)
Java

List, Set and Map in Java: How to Use Each (with Examples)