Java Tutorial · Java Basics · Lesson 2 of 37

Java Get Started

Install a JDK, then compile and run your first program.

Install a JDK

You need a JDK (Java Development Kit). Free, production-ready builds include Eclipse Temurin and Amazon Corretto. Choose Java 21 (LTS). Then check it works:

java -version
javac -version

Compile and run

Save this program as Main.java — the file name must match the public class name:

Example 1
public class Main {
    public static void main(String[] args) {
        System.out.println("Java version: " + Runtime.version().feature());
        System.out.println("It works!");
    }
}

Output

Java version: 21
It works!

Run this on your computer — see “Get Started” for setup.

The classic way is two steps — compile to bytecode, then run:

javac Main.java     # creates Main.class
java Main           # runs it

Since Java 11 you can run a single file directly:

java Main.java

IDEs and build tools

For real projects, use an IDE such as IntelliJ IDEA (Community edition is free) or VS Code with the Java extensions, and a build tool — Maven or Gradle — to manage dependencies.

Test yourself

If your public class is called Main, what must the file be named?

  1. main.java
  2. Main.java
  3. Main.class
  4. Program.java
Show answer

Answer: Main.java