Java Installation
Java is one of the most widely used programming languages in the world, powering desktop applications, web servers, enterprise software, Android apps, big-data systems, and much more. Before you can write your first Java program, you must correctly install Java on your computer. A proper installation ensures you have the compiler, interpreter, libraries, environment variables, and tools required to run and develop Java applications.
This guide provides a complete, beginner-friendly, and in-depth explanation of how to install Java on Windows, macOS, and Linux. It also explains the difference between JDK and JRE, how to verify your installation, how to configure environment variables, common errors, and tips for selecting the right Java version. By the end of this article, you will have a solid understanding of the Java installation process and will be ready to start coding.
1. Understanding Java Installation (Before You Begin)
Before diving into the installation steps, it’s important to know what exactly you are installing and why.
1.1 JDK vs JRE: What’s the Difference?
Java Development Kit (JDK)
- A complete toolkit for developing Java applications.
- Includes the Java compiler (javac), interpreter (java), libraries, JVM, debugging tools, and development utilities.
- Required for learning and writing Java code.
Java Runtime Environment (JRE)
- Allows users to run Java applications, but not develop them.
- Includes JVM + standard runtime libraries.
- Not sufficient for programmers.
Beginners, students, and developers must install the JDK.
2. Choosing the Right Java Version
There are multiple Java distributions and versions available. Before installing Java, choose the one that fits your needs.
2.1 Long-Term Support (LTS) Versions
LTS versions are stable and supported for many years. Examples include:
- Java 8 (very popular for enterprise apps)
- Java 11
- Java 17
- Java 21
2.2 Popular Java Distributions
You can download Java from several providers:
| Distribution | Description |
|---|---|
| Oracle JDK | Official distribution, widely used, high performance |
| OpenJDK | Fully open-source version of Java |
| Eclipse Temurin | Trusted, community-supported OpenJDK build |
| Amazon Corretto | Free, production-ready OpenJDK |
| Zulu JDK | Free Java distribution with wide platform support |
3. How to Install Java on Windows (Step-by-Step Guide)
Windows is the most commonly used platform for Java beginners, so this section provides a detailed walkthrough.
3.1 Step 1 – Download the JDK
- Visit any of the following official sites:
- Oracle JDK
- Eclipse Temurin
- Amazon Corretto
- Choose:
- Operating System: Windows
- Architecture: x64
- Version: (e.g., Java 17 or Java 21)
- Download the .msi or .exe installer.
3.2 Step 2 – Run the Installer
- Double-click the installer.
- Follow the installation wizard:
- Accept the license agreement.
- Choose an installation path (default is recommended).
- Complete installation.
3.3 Step 3 – Configure Environment Variables (Important)
Windows needs to know where Java is installed so you can use it from any directory.
3.3.1 Setting JAVA_HOME
- Open Start Menu → Edit the system environment variables
- Click Environment Variables
- Under System Variables, click New
- Variable Name: JAVA_HOME
- Variable Value: Example (modify according to version):
C:\Program Files\Java\jdk-21
Click OK.
3.3.2 Add Java to PATH
- Under System Variables, find Path
- Click Edit → New
- Add: %JAVA_HOME%\bin
- Click OK to save.
3.4 Step 4 – Verify Java Installation
Open Command Prompt and run:
java -version
Expected output:
java version “21.0.x”
Java(TM) SE Runtime Environment
Java HotSpot(TM) 64-Bit Server VM
Now check the compiler:
javac -version
If both commands display the version correctly, Java is installed successfully.
4. How to Install Java on macOS
4.1 Step 1 – Download the JDK
Go to the official Java distribution site and download the .pkg installer for macOS.
4.2 Step 2 – Install Using the Installer
- Open the .pkg file.
- Follow the installation wizard.
- Java will be installed in /Library/Java/JavaVirtualMachines/
4.3 Step 3 – Verify Installation
Open Terminal:
java -version
javac -version
If versions appear, Java is installed successfully.
5. Installing Java on Linux (Ubuntu, Debian, Fedora, CentOS)
Linux users usually install Java through package repositories.
5.1 Install OpenJDK on Ubuntu/Debian
To install Java 21 LTS:
- sudo apt update
- sudo apt install openjdk-21-jdk
For Java 17:
sudo apt install openjdk-17-jdk
5.2 Install Java on Fedora/CentOS
sudo dnf install java-21-openjdk-devel
5.3 Verify Installation
java -version
javac -version
6. Setting Up JAVA_HOME on Linux/macOS
6.1 Find Java Path
which java
Or:
/usr/libexec/java_home
6.2 Add to .bashrc or .zshrc:
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
Save the file and run:
source ~/.bashrc
7. How to Install Multiple Java Versions (Advanced)
Developers often need more than one Java version on their system—for example, Java 8 for older projects and Java 21 for new ones.
7.1 Using SDKMAN (Linux/macOS)
Install SDKMAN:
curl -s “https://get.sdkman.io”
“$HOME/.sdkman/bin/sdkman-init.sh”
Check available versions:
sdk list java
Install a version:
sdk install java 21-tem
Switch between versions:
sdk use java 17-tem
sdk use java 21-tem
7.2 Using Java Installer for Windows
Windows users can install different versions in separate folders and manually switch using the PATH variable.
7.3 Using jEnv (macOS/Linux)
jenv is another tool to manage multiple JDKs.
8. Common Mistakes and How to Fix Them
8.1 “java is not recognized as an internal or external command”
Cause: Java not added to PATH.
Fix: Add the location of bin folder to PATH.
8.2 Compiler Not Found (javac missing)
Cause: Installed JRE instead of JDK.
Fix: Install full JDK.
8.3 JAVA_HOME Incorrectly Set
Ensure JAVA_HOME points to the JDK root folder, not the bin folder.
9. Testing Java Installation with a Sample Program
Create a simple Java program to ensure everything works.
9.1 Create File: HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Java installation successful!”);
}
}
9.2 Compile
javac HelloWorld.java
9.3 Run
java HelloWorld
If the text prints on screen, your Java setup is perfect.
10. Recommended IDEs After Installation
After installing Java, you need an Integrated Development Environment (IDE):
10.1 IntelliJ IDEA (Recommended)
Most powerful and popular Java IDE.
10.2 Eclipse
Lightweight, widely used in enterprise projects.
10.3 NetBeans
Good IDE for beginners.
10.4 VS Code with Java Extensions
Lightweight code editor with good Java support.
11. Frequently Asked Questions (FAQ)
Q1. Should I install JDK or JRE?
Always install JDK for development.
Q2. Which Java version is best for beginners?
Java 17 LTS or 21 LTS.
Q3. Can I install more than one JDK?
Yes, and you can switch between them.
Q4. Is Java free?
OpenJDK, Temurin, and Corretto are free.
Q5. What if the installation fails?
Reinstall Java, reset PATH, and verify using java -version.
12. Bonus: Useful Java CLI Tools
javac– Compiles.javafilesjava– Runs compiled.classfilesjavadoc– Generates documentationjar– Packages Java files into a.jararchive
Conclusion
Installing Java is the first major step in your programming journey. Whether you are learning Java for backend development, Android applications, desktop programs, or competitive programming, the JDK is the foundation for everything. Correct installation ensures that your tools work smoothly, your IDEs integrate properly, and your programs execute without errors.
This detailed guide covered every aspect of Java installation—from choosing the right version to configuring environment variables, installing on different operating systems, handling multiple Java versions, and troubleshooting common issues. With Java successfully installed, you are now ready to move forward with writing Java programs, exploring object-oriented concepts, and building your first applications.