What the Java Development Kit is and why you might need it
The Java Development Kit (JDK) is a set of tools that lets you write, test, and run programs written in the Java programming language. If you're building software, working with certain development frameworks, or running applications that require Java compilation, you'll need the JDK installed on your machine. It's different from the Java Runtime Environment (JRE), which only runs Java programs — the JDK lets you create them.
You might arrive at needing the JDK because a project you're working on requires it, a tutorial you're following assumes it's there, or a build tool like Maven or Gradle won't run without it. On Windows, macOS, and Linux, the installation process is straightforward once you know which version to download and where to put it.
Key Takeaways
- Download the JDK from Oracle's official website or use an open-source version like OpenJDK, which works identically for most purposes.
- On Windows, run the installer executable and note the installation folder — you'll need it to set environment variables.
- On macOS and Linux, extract the downloaded file to a standard location like /usr/local/java or your home directory.
- After installation, verify the JDK works by opening a terminal or command prompt and typing java -version and javac -version.
- If commands don't work, you need to set the JAVA_HOME environment variable so your system knows where to find the JDK.
Choosing between Oracle JDK and OpenJDK
Oracle publishes the official JDK, but OpenJDK is a free, open-source version that works identically for nearly all development work. Oracle's JDK requires a license for commercial use, while OpenJDK is free for any purpose. For learning, personal projects, or most professional work, OpenJDK is the practical choice.
If you're following a tutorial or working on a team project, check whether they specify a particular version. Otherwise, download the latest Long-Term Support (LTS) version — currently Java 21 or Java 17 — from either oracle.com/java/technologies/downloads (for Oracle JDK) or openjdk.java.net (for OpenJDK). Both give you the same core tools.
Installing on Windows
Download the Windows installer (.exe file) from your chosen source. Run the installer and follow the prompts. When asked where to install, note the folder path — it typically defaults to something like C:\Program Files\Java\jdk-21. You can accept the default location or choose your own, but remember the exact path.
After installation completes, you need to set the JAVA_HOME environment variable so programs can find your JDK. Right-click "This PC" or "My Computer" on your desktop, select Properties, then click "Advanced system settings." In the System Properties window, click "Environment Variables" at the bottom. Under "System variables," click "New" and create a variable named JAVA_HOME with the value set to your JDK installation folder (for example, C:\Program Files\Java\jdk-21). Click OK to save.
Next, find the Path variable in the same Environment Variables window, select it, and click "Edit." Click "New" and add %JAVA_HOME%\bin. This tells Windows where to find the java and javac commands. Click OK and close all windows.
To verify installation worked, open Command Prompt (search for "cmd" in the Start menu), type java -version, and press Enter. You should see the Java version number. Then type javac -version to confirm the compiler is available. If both commands show version information, your JDK is ready to use.
Installing on macOS
Download the macOS installer (.dmg file) from Oracle or OpenJDK. Open the .dmg file and run the installer package inside. The installer will guide you through the process and place the JDK in /Library/Java/JavaVirtualMachines/ by default.
After installation, open Terminal (find it in Applications > Utilities) and type java -version. If you see the version number, macOS has already found your JDK. If not, you need to set JAVA_HOME. In Terminal, type nano ~/.zshrc (or ~/.bash_profile if you use an older macOS). Add this line at the end of the file:
export JAVA_HOME=$(/usr/libexec/java_home)
Press Control+O to save, then Control+X to exit. Close Terminal and open a new Terminal window. Type java -version and javac -version to confirm both work.
Installing on Linux
On Ubuntu or Debian-based systems, you can install OpenJDK directly from the package manager. Open a terminal and type sudo apt update, then sudo apt install openjdk-21-jdk (replace 21 with your preferred version). The system will download and install everything automatically.
If you're downloading the JDK manually, download the Linux .tar.gz file, open a terminal, and navigate to your Downloads folder. Extract it with tar -xzf jdk-21_linux-x64_bin.tar.gz (use your actual filename). Move the extracted folder to a standard location like /usr/local/java by typing sudo mv jdk-21 /usr/local/java.
Next, set JAVA_HOME. Open your shell configuration file with nano ~/.bashrc (or ~/.zshrc if you use zsh). Add these lines at the end:
export JAVA_HOME=/usr/local/java/jdk-21export PATH=$JAVA_HOME/bin:$PATH
Save and exit, then type source ~/.bashrc to apply the changes. Verify with java -version and javac -version.
What to do if commands don't work after installation
If you type java -version and see "command not found," your system can't locate the JDK. The most common cause is that JAVA_HOME isn't set or is pointing to the wrong folder. Double-check the path you entered — it should point to the JDK folder itself, not a subfolder like bin.
On Windows, verify your environment variable by opening Command Prompt and typing echo %JAVA_HOME%. It should print the path you set. If it's blank or wrong, repeat the environment variable steps above. On macOS and Linux, type echo $JAVA_HOME to check. If it's blank, you may need to restart your terminal or computer for the changes to take effect.
If the path is correct but commands still don't work, try typing the full path directly. On Windows, type C:\Program Files\Java\jdk-21\bin\java -version (using your actual path). If that works, the problem is definitely the environment variable. If even the full path doesn't work, the JDK may not have installed correctly — try uninstalling and reinstalling.
Frequently Asked Questions
Do I need both the JDK and the JRE?
No. The JDK includes the JRE, so installing the JDK gives you everything you need to write and run Java programs. You only need the JRE alone if you want to run Java programs but never write them.
What's the difference between Java 17, Java 21, and other version numbers?
Java releases a new version every six months, but only certain versions (marked LTS, for Long-Term Support) receive updates for many years. Java 21 and Java 17 are both LTS versions and will be supported until 2029 and 2026 respectively. For most purposes, either works — pick whichever your project requires or the latest LTS if you have a choice.
Can I have multiple JDK versions installed at the same time?
Yes. Install them in different folders and switch between them by changing JAVA_HOME. This is useful if different projects require different Java versions, though most modern projects work with any recent LTS version.
Why do I need to set JAVA_HOME if I already installed the JDK?
Installation puts the JDK files on your computer, but JAVA_HOME tells your system where to find them. Build tools, IDEs, and other programs read JAVA_HOME to locate the compiler and runtime. Without it, they won't know where to look.