Command Palette

Search for a command to run...

PHASE 0Beginner ~28 min· topic 4 of 8

Topic 0.4

Install the JDK and Run Your First Program

In one line

Install a free JDK (Eclipse Temurin), check it with java -version and javac -version, then write Main.java, compile it with javac Main.java and run it with java Main. Since Java 11 you can also skip the separate compile step and run java Main.java directly.

Think of it like this

Getting a new kitchen ready. Before you can cook (run programs), you need the kitchen tools (the JDK) installed and somewhere you can reach them (the PATH). Then you write the recipe (Main.java), prepare it (javac compiles it), and cook it (java runs it). The first recipe everyone cooks is the same tiny one: printing "Hello, World!".

Words you'll meet

New words in this topic, in plain English. Come back here whenever one feels fuzzy.

Terminal
A window where you type commands and see their text output. PowerShell on Windows, Terminal on macOS and Linux.
Command
A line you type in a terminal to run a program, such as javac Main.java.
Folder (directory)
A place on disk that holds files. The terminal always has a current folder, where commands look for files.
PATH
An environment variable listing the folders where your computer looks for commands. The JDK's bin folder must be on it.
JAVA_HOME
An environment variable that points to the JDK's main folder. Build tools and IDEs use it to find Java.
Environment variable
A named setting the operating system gives every program, such as PATH or JAVA_HOME.
Compile
Translate source code into another form. javac compiles .java files into .class files.
IDE
Integrated Development Environment: an editor with Java built in, such as IntelliJ IDEA or VS Code with Java extensions. It compiles and runs for you.
Eclipse Temurin
A free, open-source JDK distribution from the Eclipse Adoptium project.

Step by step

01Option 0: use the Run button in this course

You don't need to install anything to start. Every example marked runnable has a Run button that compiles and runs it in your browser with Java 17. You can edit the code and run it again. Use it for Phases 0 to 4 if you like.

You'll still want a real JDK soon: for Java 21 examples, for reading real error messages, and because real programs live on your own machine. The rest of this topic sets that up.

02Install Temurin on Windows

Easiest: open PowerShell and use winget, the Windows package manager that comes with Windows 10 and 11. Or download the .msi installer from adoptium.net, and in the installer turn on "Set JAVA_HOME variable" and "Add to PATH".

Close and reopen the terminal afterwards. A terminal reads PATH only when it starts, so an old window won't see the new java.

terminal
$ winget install EclipseAdoptium.Temurin.21.JDK
Replace 21 with 25 for Java 25. The JDK lands in a folder like C:\Program Files\Eclipse Adoptium\jdk-21.0.4.7-hotspot.

03Install Temurin on macOS and Linux

macOS: with Homebrew installed, one command installs Temurin. /usr/libexec/java_home -V lists every JDK on the Mac and where it lives.

Linux (Ubuntu or Debian): the system package openjdk-21-jdk is an OpenJDK build that works the same for this course. Temurin's own package temurin-21-jdk is available after adding the Adoptium repository (instructions on adoptium.net). On Fedora the package is java-21-openjdk-devel. SDKMAN! (sdk install java ...) is a popular way to install and switch between many JDKs on macOS and Linux.

terminal
$ # macOS
brew install --cask temurin@21
# Ubuntu / Debian
sudo apt update
sudo apt install openjdk-21-jdk

04Check the installation

Open a new terminal and ask both tools for their version. You should see the version you installed from both. If javac is missing but java works, you installed a JRE-only package or another JDK's java is first on your PATH.

To see which java your terminal picks, use where java (Windows) or which java (macOS and Linux). If the folder isn't the one you just installed, an older Java is earlier on the PATH.

terminal
$ java -version
javac -version
── expected output ──
openjdk version "21.0.4" 2024-07-16 LTS
OpenJDK Runtime Environment Temurin-21.0.4+7 (build 21.0.4+7-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.4+7 (build 21.0.4+7-LTS, mixed mode, sharing)
javac 21.0.4

05Write Main.java

Make a folder for your work (for example java-course), and inside it create a file named exactly Main.java. Use any plain-text editor: VS Code, Notepad, TextEdit in plain-text mode, or an IDE. Don't use a word processor such as Word; it adds hidden formatting.

Type the program below exactly, including capital letters, braces and the semicolon. Topic 0.5 explains every word. For now: public class Main names the class, main is where the program starts, and System.out.println prints a line.

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

06Compile with javac, run with java

In the terminal, go to your folder with cd java-course (cd means change directory). Then compile. If javac prints nothing, it worked: silence means success. A new file Main.class appears next to Main.java.

Now run it with java Main. You type the class name Main, not Main.class and not Main.java. The JVM finds Main.class in the current folder, loads it and calls its main method.

terminal
$ cd java-course
javac Main.java
java Main
── expected output ──
Hello, World!
Compile with javac, run with javadiagram
Rendering diagram…

07The shortcut: java Main.java (Java 11+)

Since Java 11 the launcher can run a source file directly. java Main.java compiles the file in memory and runs it straight away. No Main.class is written to disk.

In this mode the file name doesn't even have to match the class name, and the first class in the file is the one that runs, so it must contain the main method. (Java 22 and later can also launch programs spread over several source files.) Use it for experiments and scripts. For bigger projects, compile properly so you only recompile what changed.

terminal
$ java Main.java
── expected output ──
Hello, World!

08Choose an editor

You can write Java in any text editor, but an IDE saves a lot of time: it shows errors as you type, completes names, and runs programs with one click. Popular free choices are IntelliJ IDEA Community Edition and VS Code with Microsoft's "Extension Pack for Java". Both find your installed JDK.

Learn the terminal way first anyway. When an IDE hides an error behind a red squiggle, knowing what javac and java actually do lets you understand it.

Try it yourself

  1. 1

    Install and verify

    Install Temurin 21 or 25 for your operating system, open a new terminal, and run java -version and javac -version. Both should report the same feature version.

    terminal
    $ javac -version
    ── expected output ──
    javac 21.0.4
  2. 2

    Compile, list, run

    Create Main.java with Hello World, compile it, and list the folder (dir on Windows, ls on macOS and Linux) to see the new Main.class. Run it with java Main.

    terminal
    $ javac Main.java
    ls
    java Main
    ── expected output ──
    Main.class Main.java
    Hello, World!
  3. 3

    Make it yours

    Change the program to print your own name card, like the third example. Predict exactly what will appear, including blank lines, then run it with java Main.java.

Code & diagrams

Hello, World! New tab

The classic first program. Change the text between the quotes and run it again.

Sign in to run this example in your browser.

Expected output

Hello, World!
Your first program, a little bigger New tab

print stays on the same line; println ends the line. An empty println() prints a blank line.

Sign in to run this example in your browser.

Expected output

Hello! My name is Asha.
I am learning Java today.

2 + 3 = 5
Have a great day!
A name card made of text New tab

Programs can draw simple pictures with characters. Every line is one println.

Sign in to run this example in your browser.

Expected output

+-----------------+
|  Name: Ravi     |
|  Age:  11       |
|  Likes: chess   |
+-----------------+
The two ways to run, side by side Java 11+bash

Two-step: javac then java, leaves Main.class on disk. One-step (Java 11+): java Main.java, compiles in memory.

# Two steps (every Java version)
javac Main.java      # writes Main.class
java Main            # runs the class named Main

# One step (Java 11 and later)
java Main.java       # compiles in memory, then runs

Break it on purpose

Errors are the best teachers. Make each change, read the error, guess what went wrong, then reveal the answer.

Break #1

File name doesn't match the public class

Save the Hello World program as Hello.java (the class inside is still public class Main) and compile it with javac.

terminal
$ javac Hello.java
── what you'll see ──
Hello.java:1: error: class Main is public, should be declared in a file named Main.java
public class Main {
^
1 error

Break #2

Run the file instead of the class

After compiling, run java Main.class instead of java Main.

terminal
$ java Main.class
── what you'll see ──
Error: Could not find or load main class Main.class
Caused by: java.lang.ClassNotFoundException: Main.class

Break #3

The terminal can't find javac

Install the JDK but forget to add it to PATH, or use a terminal that was open before the install, then type javac -version.

terminal
$ javac -version
── what you'll see ──
javac : The term 'javac' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
That is PowerShell. Old cmd.exe says: 'javac' is not recognized as an internal or external command, operable program or batch file. macOS and Linux say: bash: javac: command not found (or zsh: command not found: javac).

Myth vs fact

Myth

I have to pay for Java.

Fact

OpenJDK builds such as Eclipse Temurin, Amazon Corretto and Microsoft's build are free to use, including at work. Some vendors sell support contracts, which are optional.

Myth

java Main.java is a different, weaker kind of Java.

Fact

It is the normal compiler and JVM. The launcher just compiles in memory first. The program runs exactly as if you had used javac then java.

Myth

I need an IDE to write Java.

Fact

A text editor and a terminal are enough. IDEs make you faster, and you'll want one for big projects, but they run the same javac and java underneath.

Pro corner

Extra depth for experienced readers. New to this? Skip it for now and come back later.

  • ▸

    Keep several JDKs side by side and switch per project: SDKMAN! (sdk use java ...) on macOS and Linux, /usr/libexec/java_home -v 21 on macOS, or setting JAVA_HOME and PATH per shell on Windows. Build tools can pin a toolchain version independently of the shell (Gradle toolchains, Maven toolchains), which avoids "works on my machine" version drift.

  • ▸

    Single-file source launch (JEP 330, Java 11) compiles in memory and loads the result with an in-memory class loader, so no class file is written. It also supports shebang scripts on Unix: a file starting with #!/usr/bin/java --source 21 can be made executable and run directly. Java 22 (JEP 458) extended it to multi-file programs.

  • ▸

    java searches the class path for Main.class. By default that's the current folder, unless the CLASSPATH environment variable is set, which silently replaces the default. A stale CLASSPATH is a classic reason for ClassNotFoundException in a folder that clearly contains the class; java -cp . Main overrides it.

Remember this

  1. 1

    The JDK is the one thing you install. This course recommends Eclipse Temurin from adoptium.net: free, open source, available for Windows, macOS and Linux, with long-term updates. Pick an LTS version: 21 or 25 (Topic 0.3). Choose the JDK package, not a JRE, because you need the compiler.

  2. 2

    A terminal (also called a command line or shell) is a window where you type commands instead of clicking. On Windows it is PowerShell or Terminal, on macOS it is Terminal, on Linux it is usually a terminal app running bash. Everything in this topic is typed there.

  3. 3

    Your computer finds commands like java by searching the folders listed in an environment variable called PATH. Installers normally add the JDK's bin folder to PATH. Many tools (Maven, Gradle, IDEs) also look for JAVA_HOME, which should point to the JDK folder itself (the folder that contains bin).

  4. 4

    The classic way has two steps. javac Main.java compiles your source into Main.class. java Main starts the JVM and runs the class named Main. Notice that java takes the class name, not the file name: no .class on the end.

  5. 5

    Since Java 11, java Main.java does both steps at once: it compiles the file in memory and runs it, leaving no .class file behind. It is perfect for small programs and for learning. Real projects with many files use the two-step way, usually through a build tool (Topic 15.4).

  6. 6

    Java has a few strict rules that trip up every beginner on day one: the file name must match the public class name exactly (Main.java for public class Main), capital letters matter (Main and main are different), and every statement ends with a semicolon. Topic 0.5 explains each part of the program, and Topic 0.7 explains the error messages.

Explain it without notes

01

What is the difference between javac Main.java, java Main and java Main.java?

02

What are PATH and JAVA_HOME, and why does Java need them?

03

Why must the file holding public class Main be named Main.java?

04

You ran java -version and got 17, but you installed 21. What is going on and how do you fix it?

Practice

01

Write a program that prints a three-line greeting: your name, your favourite food, and your favourite colour.

02

Write a program that prints the word Java on one line using four separate System.out.print calls, one letter each, then ends the line.

03

Write a program that draws a small triangle of stars, three lines high (*, **, ***).

Trade-offs

  • ↔

    java Main.java is fastest to type and leaves no files around, but it recompiles every time and handles one file well. javac plus java compiles once and scales to many files. Real projects use a build tool on top.

  • ↔

    Installing with a package manager (winget, Homebrew, apt, SDKMAN!) makes upgrades one command, while a manual installer gives you full control of the location. Package managers are usually the better default.

Done when you can

  • Done when java -version and javac -version both show the JDK you installed.

  • Done when you can explain what PATH and JAVA_HOME are for.

  • Done when you can compile with javac Main.java and run with java Main.

  • Done when you can run a single file with java Main.java and say what is different.

  • Done when you can fix a file-name mismatch and a "command not found" error.