To write and run Java programs you need the Java Development Kit (JDK). It includes:
💡 Which version? Always prefer a Long Term Support (LTS) release for stability. The current LTS versions are Java 17 and Java 21. Java 21 is recommended for new projects.
| Version | Type | Release Date | Support Until | Recommendation |
|---|---|---|---|---|
| Java 8 | LTS | Mar 2014 | Dec 2030 | Legacy projects only |
| Java 11 | LTS | Sep 2018 | Sep 2026 | Still widely used |
| Java 17 | LTS | Sep 2021 | Sep 2029 | Stable choice |
| Java 21 | LTS | Sep 2023 | Sep 2031 | ✅ Recommended |
Go to Oracle JDK downloads or Adoptium (OpenJDK). Choose Windows x64 Installer (.msi) for Java 21.
Double-click the downloaded
.msi file. Click
Next through the wizard and accept the default
install path:
C:\Program Files\Java\jdk-21
JAVA_HOME
C:\Program Files\Java\jdk-21
Path variable ? click
Edit ? click New ? add:
%JAVA_HOME%\bin
Open Command Prompt and run:
java -version javac -version
Expected output:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install openjdk@21
sudo ln -sfn $(brew --prefix openjdk@21)/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-21.jdk echo 'export JAVA_HOME=$(brew --prefix openjdk@21)' >> ~/.zshrc source ~/.zshrc
.pkg from Oracle or Adoptium for
Java 21.
.pkg and follow the installer
wizard.
echo 'export JAVA_HOME=$(/usr/libexec/java_home -v 21)' >> ~/.zshrc source ~/.zshrc
java -version javac -version
sudo apt update sudo apt install openjdk-21-jdk -y
echo 'export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64' >> ~/.bashrc echo 'export PATH=$JAVA_HOME/bin:$PATH' >> ~/.bashrc source ~/.bashrc
sudo dnf install java-21-openjdk-devel -y
sudo pacman -S jdk21-openjdk
java -version javac -version
Open a terminal (or Command Prompt on Windows) and run these two commands:
java -version javac -version
Both commands must return a version number. If you get
"command not found" or "not recognized", the
PATH is not set correctly — revisit Step
3 of your OS section above.
⚠️ Common mistake: Installing JRE
instead of JDK. The JRE does not include
javac. Make sure you downloaded the
JDK.
Once Java is installed, confirm everything works end-to-end:
HelloWorld.java
with this content:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Java is installed and working!");
}
}
javac HelloWorld.java
This creates a HelloWorld.class file
in the same folder.
java HelloWorld
✅ You're all set! Java is correctly installed and your first program ran successfully.
| Feature | Oracle JDK | OpenJDK (Adoptium / Amazon Corretto) |
|---|---|---|
| License | Free for dev/testing; paid for production | 100% free and open source |
| Performance | Identical | Identical |
| Long-term support | Yes (Oracle subscription) | Yes (community / vendor) |
| Recommended for | Oracle-supported enterprise environments | Most developers and projects |
📌 For personal learning and most professional projects, OpenJDK from Adoptium (Eclipse Temurin) is the best choice — fully free with LTS support.
If you need to switch between Java versions (e.g. Java 11 for an old project and Java 21 for a new one), use a version manager:
sdk install java 21-tem then
sdk use java 21-tem
JAVA_HOME to point to the desired
version folder
# SDKMAN example (macOS/Linux) sdk list java # see available versions sdk install java 21-tem sdk install java 17-tem sdk use java 17-tem # switch for current session sdk default java 21-tem # set global default