Java Fundamentals

Installing Java & Your First Program

15 min Lesson 2 of 14

Installing Java & Your First Program

In the previous lesson you learned what Java is and how the JVM works. Now it is time to get Java running on your machine and write your very first program. By the end of this lesson you will have the JDK installed, understand the difference between javac and java, and know exactly what every line of a Hello World class does.

Installing the JDK

Java programs need the JDK (Java Development Kit) to be compiled and run. There are several distributions; the most widely used are:

  • Oracle JDK — the official release from Oracle. Free for development; requires a license for commercial production use beyond Java 17.
  • OpenJDK — the fully open-source reference implementation. Free for all use. Most Linux distributions ship it via their package managers.
  • Eclipse Temurin (Adoptium) — a community-built OpenJDK distribution, highly recommended for beginners and professionals alike.
Which version should you install? Always pick the latest LTS (Long-Term Support) release. At the time of writing that is Java 21. LTS releases receive security and bug-fix updates for years, making them safe for learning and production alike.

Step-by-step installation:

  1. Go to adoptium.net (Eclipse Temurin) and download the JDK 21 installer for your operating system.
  2. Run the installer and accept the defaults. On Windows, tick the option to set JAVA_HOME and add Java to the PATH.
  3. Open a terminal (Command Prompt / PowerShell on Windows, Terminal on macOS/Linux) and verify the installation:
java -version

You should see output like:

openjdk version "21.0.3" 2024-04-16 OpenJDK Runtime Environment Temurin-21.0.3+9 (build 21.0.3+9) OpenJDK 64-Bit Server VM Temurin-21.0.3+9 (build 21.0.3+9, mixed mode, sharing)
What is JAVA_HOME? It is an environment variable that points to the JDK installation directory. Many tools — build systems like Maven and Gradle, IDEs, and CI servers — look for JAVA_HOME to locate the JDK. Setting it during installation saves you from headaches later.

javac vs java — Two Separate Tools

The JDK ships with two command-line programs you will use constantly:

  • javac — the Java compiler. It reads your .java source files and produces .class files containing bytecode.
  • java — the Java launcher. It starts the JVM and runs a compiled .class file (or a JAR archive).

The workflow is always: write → compile with javac → run with java. This two-step process is what lets the same bytecode run on any operating system with a JVM installed.

Writing Your First Program

Create a new file called HelloWorld.java and type the following exactly:

public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
The filename must match the class name exactly, including capitalisation. If your class is named HelloWorld, the file must be saved as HelloWorld.java. A mismatch causes a compiler error.

Anatomy of main()

Let us break down every keyword on that main line, because each one is required:

  • public — the JVM launcher must be able to call this method from outside the class, so it must be public.
  • static — the JVM calls main before any objects exist. static means the method belongs to the class itself, not to an instance.
  • voidmain does not return a value to the JVM.
  • main — the exact name the JVM looks for as the entry point of every Java program.
  • String[] args — an array of String values passed in from the command line. You can ignore this for now, but the signature must include it.

Inside main, System.out.println(...) prints a line of text to the console. System is a built-in class, out is a static field of type PrintStream, and println is a method that prints the argument followed by a newline.

Compiling and Running

Open your terminal, navigate to the folder where you saved HelloWorld.java, and run these two commands:

# Step 1 — compile: produces HelloWorld.class in the same folder javac HelloWorld.java # Step 2 — run: notice no .class extension and no file path, just the class name java HelloWorld

You should see:

Hello, World!
Java 11+ single-file shortcut: For a file containing only one class you can skip the compile step and run it directly with java HelloWorld.java. This is handy for quick experiments, but under the hood the JVM still compiles to bytecode — it just does it in memory. For any real project, always use javac explicitly.

What Happens Step by Step

  1. javac HelloWorld.java reads your source, checks for syntax errors, and writes HelloWorld.class (bytecode).
  2. java HelloWorld starts the JVM, loads HelloWorld.class, finds the public static void main(String[] args) method, and calls it.
  3. The JIT compiler inside the JVM converts the most-used bytecode to native machine instructions on the fly for performance.
  4. System.out.println writes to standard output, and you see the text in your terminal.

Recommended IDEs

While you can write Java in any text editor, a proper IDE makes development much faster. The two most popular choices are:

  • IntelliJ IDEA Community Edition — free, arguably the best Java IDE. Auto-completes, highlights errors in real time, and runs your program with a single click.
  • VS Code + Extension Pack for Java — lightweight alternative if you prefer VS Code.

Both options detect your installed JDK automatically. From now on, lessons will assume you are using either the terminal or an IDE — both approaches work identically.

Summary

You installed the JDK, confirmed it works with java -version, learned that javac compiles and java runs, wrote a HelloWorld class, and dissected every keyword in the main method signature. This two-step compile-then-run workflow is the foundation of everything you will do in Java.