Every Java program runs on the JVM (Java Virtual Machine), which is what makes Java "write once, run anywhere" - the same compiled code runs on Windows, Mac, or Linux without changes. To write Java, you need the JDK (Java Development Kit), which includes the compiler and the JVM together.

Installing the JDK

Download a JDK (Java 17 or later is a safe modern choice) from Oracle or Eclipse Adoptium, install it, then confirm it worked by opening a terminal and running:

java -version
javac -version

If both commands print a version number, you are ready to go.

Your first program

Create a file called HelloWorld.java - the filename must exactly match the public class name inside it:

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

Compiling and running

Java is compiled, not interpreted directly - you turn your .java source file into bytecode with javac, then run that bytecode with java:

javac HelloWorld.java
java HelloWorld

The first command produces a HelloWorld.class file (the compiled bytecode); the second runs it on the JVM. You should see Hello, World! printed to your terminal.

What just happened

public class HelloWorld defines a class - everything in Java lives inside a class. public static void main(String[] args) is the entry point every Java program needs - it is the method the JVM calls first. System.out.println(...) prints text followed by a new line.