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:
- the compiler
javacchecks your fileHello.javaand translates it into bytecode, an intermediate code stored in a.classfile; - the Java virtual machine (JVM), started by the
javacommand, 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.
Text description of the animation
- You write your program in a file whose name ends in .java.
- The compiler checks the code. Here, everything is correct.
- If it finds an error, it stops and gives the line. Nothing is run.
- The bytecode is produced. It can run on Windows, macOS or Linux.
- The JVM loads the bytecode and runs it.
- 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
Hello.java: three lines IO.println("..."); are enough.Hint 2
".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.printlnorIO.Println: Java is case-sensitive. It'sIO.println. - Using curly quotes (
“ ”) copied from a word processor: Java doesn't recognise them. Type straight quotes in your code editor.
Summary
- 1javac translates Java code into bytecode; the JVM (the java command) runs it.
- 2Bytecode is the same on every system: only the JVM changes.
- 3With Java 25, java File.java compiles and runs in a single command.
- 4Faced with an error, read the first one: file, line, cause.
- 5Java is case-sensitive.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.