Checking lesson access...
Setting Up the Java Development Environment
Install OpenJDK 21 (Eclipse Temurin build), check it from the terminal, set `PATH` and `JAVA_HOME`, and configure JDK 21 in IntelliJ IDEA.
You are viewing a free preview lesson.
Lesson Overview
To write, compile, and run Java code, you need a working development environment on your computer. In this course, we will use:
- OpenJDK 21, specifically the Eclipse Temurin build
- IntelliJ IDEA
As we saw in lesson 1.03, several vendors publish builds of OpenJDK. We use Eclipse Temurin, a free build of OpenJDK 21 from the Eclipse Adoptium project. In this lesson, you will install it, verify the installation, and configure IntelliJ IDEA to use JDK 21.
A careful setup saves hours later: many early "my code does not run" problems are really a wrong Java version, a terminal that cannot find javac, or an IDE using a different JDK. By the end of this lesson, you will have run a program from both the terminal and IntelliJ IDEA, and you will know where to look when one of them fails.
Learning Objectives
After completing this lesson, you will be able to:
- Install OpenJDK 21
- Verify a Java installation from the terminal
- Understand the roles of
PATHandJAVA_HOME - Configure JDK 21 in IntelliJ IDEA
- Compile and run a Java program from the terminal
- Run a Java program from IntelliJ IDEA
- Identify common setup problems
What Do You Need?
To start Java development, you need two main tools:
- OpenJDK 21
- IntelliJ IDEA
Conceptually, the setup looks like this:
Computer
├── OpenJDK 21
│ ├── Java Compiler
│ ├── Java Runtime
│ └── Development Tools
│
└── IntelliJ IDEA
├── Code Editor
├── Project Management
├── Run / Debug Support
└── Test Support
We will use Java 21 for the code and exercises in this course.
Installing OpenJDK 21
Open adoptium.net and go to the Temurin downloads. On the download page, make sure you select:
- Version: 21 (LTS)
- Correct operating system
- Correct CPU architecture
- Package type: JDK, not JRE (the JRE package has no
javac, as we saw in lesson 1.04) - The installer format for your system:
.msion Windows,.pkgon macOS, or.tar.gzon Linux
The page layout may change over time, but these choices stay the same.
CPU Architecture
The download page uses two architecture names. In general:
- Intel or AMD 64-bit processor →
x64 - Apple Silicon Mac or other ARM 64-bit processor →
aarch64
On macOS or Linux, you can check your architecture using:
uname -m
If the output is x86_64, choose x64. If it is arm64 or aarch64, choose aarch64.
Installing OpenJDK 21 on Windows
On the Temurin download page, select Windows, architecture x64 (most Windows PCs), package type JDK and version 21. Download the .msi installer and run it. During setup, you can choose which features to install. Leave Add to PATH enabled; it is on by default. You can also enable Set JAVA_HOME variable, which is off by default. We will see what JAVA_HOME means later in this lesson. The installer puts the JDK in a folder such as C:\Program Files\Eclipse Adoptium\jdk-21.0.x.y-hotspot\, where the exact numbers depend on the version you downloaded.
After installation, open a new PowerShell or Windows Terminal window and run java -version and javac -version (explained under Verify the Installation below). Both should show Java version 21.
Installing OpenJDK 21 on macOS
First, check your architecture with uname -m: an Apple Silicon Mac usually prints arm64, an Intel Mac x86_64. Then, on the Temurin download page, select macOS, architecture aarch64 for Apple Silicon or x64 for Intel, package type JDK and version 21. Download the .pkg installer, open it, and follow the steps. It installs the JDK into /Library/Java/JavaVirtualMachines/temurin-21.jdk. You can view installed JDKs using:
/usr/libexec/java_home -V
Then verify the installation as shown under Verify the Installation below.
Installing OpenJDK 21 on Linux
On Linux, you have two common options. The first is to download the .tar.gz archive from the Temurin download page (operating system Linux, your architecture, package type JDK, version 21), extract it to a folder of your choice, and add its bin directory to PATH.
The second is to install it with your package manager from Adoptium's package repositories, where the package is called temurin-21-jdk. Adoptium's installation instructions explain how to add the repository for your distribution. Many distributions also package OpenJDK 21 themselves (for example, openjdk-21-jdk on Ubuntu and Debian), and that is also fine for this course.
After installation, verify it as shown in the next section. Both commands should report Java 21.
Verify the Installation
After installing the JDK, run both commands:
java -version
javac -version
The java command shows whether the Java runtime is available, and the javac command shows whether the Java compiler is available. With Temurin, the output of java -version looks like this (the exact version numbers and date will differ on your machine):
openjdk version "21.0.x" 20xx-xx-xx LTS
OpenJDK Runtime Environment Temurin-21.0.x+y (build 21.0.x+y-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.x+y (build 21.0.x+y-LTS, mixed mode, sharing)
The first line must show 21, and javac -version should print something like javac 21.0.x. The major version of both should be 21. If the two commands show different Java versions, you may have multiple JDKs installed or your environment configuration may not be correct.
What Is PATH?
PATH is an environment variable. When you type java in the terminal, the operating system needs to know where the java executable is located. JDK executable files are usually located inside the bin directory:
OpenJDK 21
└── bin
├── java
├── javac
└── ...
If the JDK is installed but you still see java: command not found (or 'java' is not recognized on Windows), you should check whether your PATH configuration is correct.
What Is JAVA_HOME?
JAVA_HOME is an environment variable that usually points to the JDK installation directory, conceptually JAVA_HOME=/path/to/jdk-21. It should point to the root directory of the JDK, the folder that contains bin, not the bin directory itself. Maven, Gradle
, and other development tools may use JAVA_HOME to locate the JDK.
Checking JAVA_HOME
Windows PowerShell:
echo $env:JAVA_HOME
Windows Command Prompt:
echo %JAVA_HOME%
macOS or Linux:
echo $JAVA_HOME
If JAVA_HOME is configured, make sure it points to the JDK 21 installation folder. For Temurin, that is usually:
- Windows:
C:\Program Files\Eclipse Adoptium\jdk-21.0.x.y-hotspot - macOS:
/Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home, which$(/usr/libexec/java_home -v 21)also prints - Linux: the folder you extracted, or the one your package manager chose
Which Java Executable Is Being Used?
If multiple Java installations exist on your computer, you can check which executable the terminal is using. On Windows:
where.exe java
where.exe javac
On macOS or Linux:
which java
which javac
If the path points to an older installation, that installation comes first in PATH. Fix PATH (or uninstall the old JDK), open a new terminal and run java -version and javac -version again until both report Java 21.
Test a Java Program from the Terminal
Before configuring IntelliJ IDEA, let's compile and run a small Java program from the terminal. Create a file named Main.java:
public class Main {
public static void main(String[] args) {
System.out.println("Java setup is working");
}
}
Compile it:
javac Main.java
If compilation succeeds, a file named Main.class will be created in the same directory. Now run it:
java Main
Output:
Java setup is working
If you see this output, both your Java compiler and runtime are working.
What Is an IDE?
IDE — Integrated Development Environment is an application that provides several software development tools in one place: a code editor with code completion and error highlighting, a project explorer, run and debug support, refactoring, a test runner, a terminal and version control integration. In this course, we will use IntelliJ IDEA.
Installing IntelliJ IDEA
Download and install IntelliJ IDEA (its free tier is enough for this course) for your operating system and CPU architecture. Since version 2025.3, IntelliJ IDEA is a single product, so there is no separate Community Edition to look for. After installation is complete, start IntelliJ IDEA.
Configure JDK 21 in IntelliJ IDEA
IntelliJ IDEA needs to know which JDK it should use to compile and run your project. When creating a new Java project, select the Temurin 21 entry as the JDK. IntelliJ IDEA lists JDKs by version and vendor, so it may appear as temurin-21 or 21 Eclipse Temurin, depending on your IntelliJ IDEA version:
New Project
→ Java
→ JDK
→ temurin-21
IntelliJ IDEA usually detects the installed JDK automatically. If it does not, select Add JDK and point IntelliJ IDEA to the JDK 21 installation folder listed in the JAVA_HOME section above. On macOS, you can choose /Library/Java/JavaVirtualMachines/temurin-21.jdk. Another option is Download JDK, where you choose version 21 and vendor Eclipse Temurin, and IntelliJ IDEA downloads it for you.
What Is Project SDK?
Project SDK is the JDK that IntelliJ IDEA uses to compile and run a specific project. For projects in this course, set it to the Temurin 21 JDK (for example, temurin-21). If there is an option to select the Java language level, keep it compatible with Java 21 as well.
Test the Setup from IntelliJ IDEA
Create a Java project, select the Temurin 21 JDK, and create a Main class:
public class Main {
public static void main(String[] args) {
System.out.println("IntelliJ IDEA is ready");
}
}
Run the program. Output:
IntelliJ IDEA is ready
If you see this output, the JDK is configured, the project compiles and the program runs: your development environment is ready.
Why Did We Test Both the Terminal and IDE?
Running a program from the terminal confirms that the operating system-level Java setup is working. Running a program from IntelliJ IDEA confirms that the IDE is using the correct Project SDK. If both tests are successful, your environment is ready for the upcoming coding lessons.
Development Environment Checklist
Before moving to the next lesson, make sure:
- OpenJDK 21 (Eclipse Temurin) is installed
- The correct operating system and CPU architecture were selected
-
java -versionshows Java 21 -
javac -versionshows Java 21 - If
JAVA_HOMEis configured, it points to the JDK 21 folder - A Java program was successfully compiled from the terminal
- A Java program was successfully run from the terminal
- IntelliJ IDEA is installed
- Temurin 21 is configured as the Project SDK
- A Java program was successfully run from IntelliJ IDEA
Common Mistakes
java Command Is Not Found
If you see java: command not found or 'java' is not recognized, check that OpenJDK 21 is installed and that its bin directory is in PATH. A terminal that was already open during installation still has the old PATH, so open a new one and run java -version again.
java Works, but javac Does Not
If java -version works but javac -version fails, either a full JDK is not installed or PATH points to another Java installation that has only a runtime. Make sure you installed the JDK package of OpenJDK 21, not the JRE.
Wrong Java Version
If you see:
java 17
javac 21
or another version is being used instead of Java 21, find out which executables run with where.exe or which (as shown above). Then check PATH, JAVA_HOME and the IntelliJ IDEA Project SDK, and use Java 21 in all of them.
IntelliJ IDEA Does Not Detect the JDK
First, verify from the terminal with java -version and javac -version. If both commands work correctly, select Add JDK in IntelliJ IDEA and manually choose the JDK 21 installation folder.
Project SDK is not defined or Another Java Version in the IDE
Project SDK is not defined means no JDK has been selected for the project (the exact wording can differ between IntelliJ IDEA versions). Even when Java 21 works in the terminal, IntelliJ IDEA may also use a different JDK for a project. In both cases, set the Project SDK to the Temurin 21 JDK in the project settings.
Wrong CPU Architecture
If the installer or JVM does not start, check whether the downloaded JDK matches your CPU architecture (x64 for Intel or AMD, aarch64 for Apple Silicon and other ARM processors).
Practice Exercise 1: Verify the Environment
Solutions: lesson 1.22.
Run java -version and javac -version from the terminal and check whether both report Java 21. Then check the executable locations with where.exe java and where.exe javac on Windows, or which java and which javac on macOS and Linux.
Practice Exercise 2: Run a Program from the Terminal
Create SetupTest.java:
public class SetupTest {
public static void main(String[] args) {
System.out.println("My Java setup works");
}
}
Compile and run it:
javac SetupTest.java
java SetupTest
Expected output:
My Java setup works
Practice Exercise 3: Run a Program from IntelliJ IDEA
Create a Java project in IntelliJ IDEA, select the Temurin 21 JDK as the Project SDK, and then create:
public class EnvironmentCheck {
public static void main(String[] args) {
System.out.println("IDE and JDK are configured");
}
}
Run the program. Expected output:
IDE and JDK are configured
Knowledge Check
Question 1
Which JDK and Java version are we using in this course?
Question 2
Why do we check both java -version and javac -version?
Question 3
What is PATH used for?
Question 4
What does JAVA_HOME point to?
Question 5
What is a Project SDK?
Question 6
What commands are used to compile and run a Java program from the terminal?
Question 7
If Java 21 is available in the terminal but IntelliJ IDEA is using another Java version, where should you check?
Knowledge Check Answers
Answer 1
In this course, we use OpenJDK 21, the Eclipse Temurin build.
Answer 2
java -version shows the Java runtime version, while javac -version shows the compiler version. We need to verify that both are using Java 21.
Answer 3
PATH tells the operating system where to find executable commands.
Answer 4
JAVA_HOME points to the root directory of the JDK installation.
Answer 5
Project SDK is the JDK that IntelliJ IDEA uses to compile and run a project.
Answer 6
Compile with javac, then run with java:
javac Main.java
java Main
Answer 7
Check IntelliJ IDEA's Project SDK and configure it to use the Temurin 21 JDK.
Lesson Summary
- In this course, we use OpenJDK 21 (Eclipse Temurin) and IntelliJ IDEA.
- Download the JDK package that matches your operating system and CPU architecture.
java -versionandjavac -versionshould both report Java 21.PATHtells the operating system where to find commands such asjavaandjavac.JAVA_HOMEpoints to the root directory of the JDK, not itsbindirectory.- The Project SDK is the JDK that IntelliJ IDEA uses to compile and run a project.
- Running a program from both the terminal and IntelliJ IDEA confirms that your environment is ready.
Think first
Coming soonCheckpoint questions that ask you to commit to an answer before the lesson shows it.