Skip to content
KALANBLON
Show contents

Chapter 1 of 4 · 15 min

Installing Java and running a program

By the end of this chapter, you'll be able to

  • Install a JDK and check the installation
  • Explain the role of the javac compiler, of bytecode and of the virtual machine
  • Run a Java program with the java command
  • Read the first error displayed by the compiler

From source code to the virtual machine

A Java program goes through two stages:

  1. the compiler javac checks your file Hello.java and translates it into bytecode, an intermediate code stored in a .class file;
  2. the Java virtual machine (JVM), started by the java command, runs this bytecode on your processor.

Bytecode is the same everywhere: only the JVM changes from one system to another. That's Java's historic promise, "write once, run anywhere".

The text appears. With java Hello.java, both stages happen in a single command.

Step 6 of 6
Text description of the animation
  1. You write your program in a file whose name ends in .java.
  2. The compiler checks the code. Here, everything is correct.
  3. If it finds an error, it stops and gives the line. Nothing is run.
  4. The bytecode is produced. It can run on Windows, macOS or Linux.
  5. The JVM loads the bytecode and runs it.
  6. The text appears. With java Hello.java, both stages happen in a single command.

Installing the JDK

To write programs, you need a JDK (Java Development Kit): it contains the compiler, the JVM and the standard library. We use Eclipse Temurin 25, a free and open-source JDK. Java 25 is an LTS version (Long-Term Support): it receives fixes for several years. The examples in this course are checked with it.

  • Windows and macOS: download the Temurin 25 installer from the official website of the Adoptium project (adoptium.net), then run it, keeping the suggested options.
  • Linux (Ubuntu, Debian): sudo apt install openjdk-25-jdk. If your distribution doesn't offer this package yet, use the Temurin archive from adoptium.net.

Then open a new terminal and check:

java --version

You should see a version number starting with 25. If the command can't be found, close and reopen the terminal; on Windows, restart your session if needed.

Your first program

Create a file Hello.java in a text editor (VS Code for example):

void main() {
    IO.println("Hello, Kalanblon!");
}

In the terminal, go into the file's folder (cd path/to/folder) and run:

java Hello.java
Hello, Kalanblon!

With Java 25, the java command directly accepts a source file: it compiles it in memory, then runs it. It's the simplest way to get started. For large projects, you compile separately with javac, then run with java, or you use a build tool such as Maven or Gradle.

Your turn

Worked exercise

Worked exercise: introducing yourself

Write a program About.java that displays three lines: your first name, your city and what you'd like to build in Java. Run it with java About.java.

Hint 1
Start from Hello.java: three lines IO.println("..."); are enough.
Hint 2
Each instruction ends with a semicolon, and the text is written between straight double quotes ".
Show the solution and common mistakes
void main() {
    IO.println("First name: Diego");
    IO.println("City: Lima");
    IO.println("Project: a carpooling app");
}
First name: Diego
City: Lima
Project: a carpooling app

Common mistakes:

  • Writing io.println or IO.Println: Java is case-sensitive. It's IO.println.
  • Using curly quotes (“ ”) copied from a word processor: Java doesn't recognise them. Type straight quotes in your code editor.

Summary

  1. 1javac translates Java code into bytecode; the JVM (the java command) runs it.
  2. 2Bytecode is the same on every system: only the JVM changes.
  3. 3With Java 25, java File.java compiles and runs in a single command.
  4. 4Faced with an error, read the first one: file, line, cause.
  5. 5Java is case-sensitive.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.