Java Getting Started

Getting started with Java means installing the Java Development Kit (JDK), writing a .java file, and running it. This lesson walks through each step so you can run every example in the rest of the tutorial on your own computer.

Install the JDK

The JDK contains the compiler (javac), the runtime (java) and the standard library. Download a free OpenJDK 21 build such as Eclipse Temurin, Microsoft Build of OpenJDK, or Oracle JDK, and run the installer for your operating system.

After installing, open a terminal and check the version.

java -version
openjdk version "21.0.4" 2024-07-16 LTS
OpenJDK Runtime Environment Temurin-21.0.4+7 (build 21.0.4+7-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.4+7 (build 21.0.4+7-LTS, mixed mode, sharing)

Your exact version numbers may differ. If the command is not found, make sure the JDK bin folder is on your PATH and open a new terminal window.

Write and Run a Java Program

Create a file called Main.java. The file name must match the public class name exactly, including capital letters.

public class Main {
    public static void main(String[] args) {
        System.out.println("Java is installed and working!");
    }
}
Java is installed and working!

Compile it with javac, which creates Main.class (bytecode), then run it with java. Since Java 11 you can also skip the compile step for a single file and run the source directly.

javac Main.java
java Main

# or, in one step (Java 11+)
java Main.java
Java is installed and working!
Java is installed and working!

Choosing an Editor or IDE

  • IntelliJ IDEA Community Edition: free, the most popular Java IDE.
  • Visual Studio Code with the Extension Pack for Java: lightweight and fast.
  • Eclipse: a long-standing free IDE, common in enterprises.
  • Any plain text editor plus the terminal is enough to follow this tutorial.

JDK, JRE and JVM

The JVM (Java Virtual Machine) executes bytecode. The JRE (Java Runtime Environment) is the JVM plus the standard library, enough to run programs. The JDK is the JRE plus development tools like javac. As a developer you always install the JDK.

Frequently asked questions

What is the difference between javac and java?

javac is the compiler that turns .java source files into .class bytecode files. java is the launcher that starts the JVM and runs the compiled class.

Why does Java say "could not find or load main class"?

Usually you ran java with the wrong class name, included the .class extension, or you are in the wrong folder. Run java Main from the folder that contains Main.class.

Is the JDK free?

Yes. OpenJDK builds such as Eclipse Temurin are free for any use, and Oracle JDK 21 is also free under its current license terms.