Skip to lesson

Your first Java program

I'm on:

Showing steps for Windows. They are marked “On Windows” below.

A small Java program is a class with a main method and a few statements. Write one, save it as Hello.java and run it, in your IDE or in the terminal.

The parts of a Java program

To make the computer print a message, you write a complete program. Every Java program has three parts: a class, a main method inside it, and statements inside main.

Example · Hello.java

Hello.javaJava
public class Hello {    public static void main(String[] args) {        System.out.println("Hello, World!");        System.out.println("I am learning Java.");    }}
Output
Hello, World!
I am learning Java.

What this code does

  • public class Hello { … } is the class: the block that holds your code. Its curly brackets { } mark where it starts and ends.
  • public static void main(String[] args) is the main method: the program starts running here.
  • Each System.out.println(...) is a statement: it prints one line and ends with a semicolon ;.
  • The statements run from top to bottom, so the two lines print in that order.

Save the file with the right name

A public top-level class (one not inside another class) named Hello must be saved in a file named Hello.java, with the same capital letters: hello.java is a different name.

In an IDE, create a class named Hello and it saves Hello.java for you. Without one, type the program in Notepad or TextEdit and save it as Hello.java in a practice folder (next section).

In Notepad, set Save as type to All files. Otherwise the file may become Hello.java.txt. On a Mac, TextEdit must be in plain text mode (Format → Make Plain Text).

Open a terminal in your practice folder

Skip this if you only use your IDE. To use the terminal, keep your files in one simple folder and open the terminal there. Do this once; later just open the folder again.

On Windows

Use a folder like C:\java-practice. Your Desktop may be inside OneDrive, so cd Desktop often fails; a simple folder avoids that.

  1. Open Command Prompt. Type mkdir C:\java-practice and press Enter (only the first time).
  2. Save Hello.java into C:\java-practice.
  3. Type cd C:\java-practice and press Enter.
  4. Type dir and press Enter. You should see Hello.java in the list.
Another way: open the folder in File Explorer, click the address bar, type cmd and press Enter. Command Prompt opens in that folder.

Run it: IDE or terminal

In the terminal, type these two commands in your practice folder. They are the two steps from What is Java? (opens in a new tab): compile, then run.

Terminal
javac Hello.javajava Hello
Output
Hello, World!
I am learning Java.

In your IDE (from the previous lesson), just click Run: the IDE does both steps at once and shows the output at the bottom.

  1. javac Hello.java – the compiler checks your code and makes Hello.class (bytecode).
  2. java Hello – the JVM runs your program. Type the class name only: never java Hello.class.
Java 11 and newer can also run a single source file directly with java Hello.java. This course uses javac and then java Hello, so you see both steps.

Common beginner mistakes

Everyone gets compile errors at first. The compiler shows the file, the line number and what is wrong:

Text
Hello.java:3: error: ';' expected        System.out.println("Hello, World!")                                           ^1 error

Here line 3 has no ; at the end. Fix that line and compile again. These are the usual causes:

  • Missing semicolon – every statement, like System.out.println("Hi");, ends with ;.
  • Wrong file name – a public class Hello must be saved in Hello.java.
  • Wrong capital letters – write System, not system. Java sees them as different words.
  • Missing quotes or brackets – text needs double quotes, and every { needs a }.

If something else goes wrong, use the Common errors → fixes list in the Summary tab. Check from the top: first the JDK, then the folder and file name, then the code. Next, the programs start to store values: Variables (opens in a new tab).

Success checkpoint: java -version works, javac -version works, and your program prints Hello, World!. If all three are true, your Java setup is complete.

What's next

You finished the preview of Getting Started

Java Programming has 26 sections, 252 modules and 1949 lessons. An account opens every lesson and saves your progress.

Next step

Check your understanding

6 quick questions