Video lecture
Watch on YouTube(opens in a new tab)Sub topic 2
Download and Install the JDK
The Java Development Kit (JDK) includes the tools you need to write and run Java programs. These steps use JDK 21. If you chose another version, use that version where the steps say 21.
Install the JDK
On Windows or macOS, visit https://adoptium.net/temurin/releases/ and download the JDK for your operating system and computer architecture. Open the downloaded installer and follow its steps. On Windows, select the option to add Java to PATH if the installer offers it.
On Ubuntu 24.04, open a terminal, which is an app for entering commands, and run:
sudo apt update
sudo apt install openjdk-21-jdkThe apt tool installs software. The sudo command runs a command with administrator permission. It may ask for your password.
For other Linux systems, follow the JDK provider’s installation instructions.
Make Java Commands Available
PATH is a list of folders your computer searches for commands. A JDK installer usually makes its commands available for you. Close and reopen your terminal after installing, then check the versions below.
If Windows cannot find a Java command, search the Start menu for “Edit environment variables for your account.” Under user variables, select Path, choose Edit, and add the JDK’s bin folder. This is the folder containing java.exe and javac.exe. Save your changes and reopen the terminal.
On macOS, if the commands are missing or use the wrong JDK, run this in Terminal:
export PATH="$(/usr/libexec/java_home -v 21)/bin:$PATH"This change lasts only for the current terminal session. If it works, you can add the same line to your shell’s startup file to use it in future sessions.
On Ubuntu, the installation command above normally makes Java commands available. If they are missing, check that the installation finished without errors.
Check the Java and Compiler Versions
Run these commands in a terminal:
java -version
javac -versionThe java command starts the Java runtime, which runs Java programs. The javac command starts the compiler, which translates Java code into a form the runtime can use.
Both commands should report the version you installed, such as 21. If a command is not found, check PATH. If the versions differ, your computer may be finding tools from different JDK installations.
Exercise
What should you check if java works but javac is not found?
Show answer
Check that you installed a JDK and that its bin folder is available through PATH.