Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Java
Stage 5: Specialization

Part 01 / Topic 4 of 7

Compiling and running: javac and java

Every Java program needs at least one class, and to run it, that class needs a main method with this exact shape:

public static void main(String[] args) {
    // your code
}

Don't worry about what each word means yet (static is explained in Stage 2; for now, type it exactly like this). System.out.println(...) prints a line of text.

Real-world example: your first program, saved as Hello.java.

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

Two separate commands turn this into a running program:

$ javac Hello.java
$ java Hello
Hello, World!
Welcome to Java Step by Step
  • javac Hello.java compiles: it reads Hello.java and writes Hello.class next to it. No output means it worked.
  • java Hello runs: note there is no .java and no .class here — you give java the class name, and it finds Hello.class itself.

One hard rule: a public class must live in a file with the exact same name, including capitals: public class Hello must be in Hello.java, not hello.java or Hello.txt.

Full program: examples/Hello.java.

Done here? The practice set for Part 1 — including the solutions — is on the part page.

Search the guide