Lesson 2 +10 XP

Get Started with Java

Get Started with Java

To run Java programs on your computer you need the JDK (Java Development Kit). It includes the compiler and the runtime needed to build and run Java code.

Install the JDK

  1. Go to the Oracle or OpenJDK website and download the JDK for your system.
  2. Install it like any other program.
  3. Verify it works by opening a terminal and running:
java -version

Write your first program

Create a file named Main.java. In Java, the file name should match the class name:

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

Compile and run

First compile the file with the javac compiler:

javac Main.java

This creates a file called Main.class containing bytecode. Then run it with the java command:

java Main

The output is:

Hello World

Compile vs run

StepCommandWhat it does
Compilejavac Main.javaTurns source code into bytecode
Runjava MainRuns the compiled class on the JVM

TL;DR

  • Install the JDK and check it with java -version.
  • Save your code in a file that matches the class name.
  • Compile with javac Main.java.
  • Run with java Main.