Introduction
Java is a versatile, platform-independent programming language used for developing applications ranging from desktop software and mobile apps to enterprise systems and web services. Before you can write and run Java programs, you need to install the Java Development Kit (JDK), which includes the Java Runtime Environment (JRE), compiler (javac), and essential development tools. This guide provides step-by-step instructions for installing and configuring Java on Windows, macOS, and Linux, along with verification steps and environment setup.
1. Understanding Java Editions
- JDK (Java Development Kit): Required for developing Java applications. Includes compiler, debugger, and JRE.
- JRE (Java Runtime Environment): Required only for running Java applications (not for development).
- OpenJDK vs. Oracle JDK:
- OpenJDK: Open-source, free, and the reference implementation (recommended for most users).
- Oracle JDK: Commercially licensed (free for development, paid for production).
Recommendation: Use OpenJDK (e.g., from Adoptium, Amazon Corretto, or Azul Zulu).
2. Installing Java on Windows
Step 1: Download OpenJDK
- Go to Adoptium.net
- Select:
- Version: LTS version (e.g., JDK 17 or JDK 21)
- Operating System: Windows
- Architecture: x64 (or ARM64 if applicable)
- Package Type:
.msi(installer)
- Click Download
Step 2: Run the Installer
- Double-click the downloaded
.msifile. - Follow the installation wizard (accept defaults).
- Note the installation path (e.g.,
C:\Program Files\Eclipse Adoptium\jdk-21.0.2.13-hotspot).
Step 3: Set Environment Variables
- Open Start Menu → Search for “Environment Variables” → Edit the system environment variables.
- Click Environment Variables…
- Under System Variables, find
Path→ Edit → New. - Add the path to the
bindirectory:
C:\Program Files\Eclipse Adoptium\jdk-21.0.2.13-hotspot\bin
- Click OK to save.
Step 4: Verify Installation
Open Command Prompt and run:
java -version javac -version
✅ You should see version information for both.
3. Installing Java on macOS
Option A: Using Homebrew (Recommended)
- Install Homebrew (if not installed):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Install OpenJDK:
brew install openjdk@21 # or openjdk@17
- Link the JDK (required for
javacommand):
sudo ln -sfn /opt/homebrew/opt/openjdk@21/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-21.jdk
Option B: Manual Installation
- Download
.pkgfrom Adoptium.net - Open the
.pkgfile and follow the installer. - No environment setup needed—macOS auto-configures
javaandjavac.
Verify Installation
Open Terminal and run:
java -version javac -version
Note: On Apple Silicon Macs, ensure you download the ARM64 version.
4. Installing Java on Linux (Ubuntu/Debian)
Step 1: Update Package Index
sudo apt update
Step 2: Install OpenJDK
For JDK 21:
sudo apt install openjdk-21-jdk
For JDK 17 (LTS):
sudo apt install openjdk-17-jdk
Step 3: Set Default Java Version (if multiple versions installed)
sudo update-alternatives --config java sudo update-alternatives --config javac
Select the desired version from the list.
Step 4: Set JAVA_HOME (Optional but Recommended)
- Find Java installation path:
sudo update-alternatives --config java # Example output: /usr/lib/jvm/java-21-openjdk-amd64/bin/java
- Edit shell profile (
~/.bashrc,~/.zshrc, or~/.profile):
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64 export PATH=$JAVA_HOME/bin:$PATH
- Reload the profile:
source ~/.bashrc
Verify Installation
java -version javac -version echo $JAVA_HOME
5. Verifying Your Installation
After installation, confirm everything works:
A. Check Java and Compiler Versions
java -version # Should show runtime version javac -version # Should show compiler version
B. Write a Test Program
- Create
HelloWorld.java:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Java is working!");
}
}
- Compile and run:
javac HelloWorld.java java HelloWorld
✅ Output: Java is working!
6. Setting Up JAVA_HOME
Many tools (e.g., Maven, Gradle, Tomcat) require the JAVA_HOME environment variable.
Windows
- In Environment Variables, add a new System Variable:
- Name:
JAVA_HOME - Value:
C:\Program Files\Eclipse Adoptium\jdk-21.0.2.13-hotspot
macOS / Linux
Add to your shell profile (~/.bashrc, ~/.zshrc):
export JAVA_HOME=/path/to/your/jdk
Find the path with:
- macOS:
/usr/libexec/java_home -v 21- Linux:
readlink -f $(which java) | sed "s:bin/java::"
7. Managing Multiple Java Versions
If you work on projects requiring different Java versions, use a version manager:
curl -s "https://get.sdkman.io" | bash source "$HOME/.sdkman/bin/sdkman-init.sh" sdk install java 21-tem sdk install java 17-tem sdk use java 17-tem # Switch version
8. Common Issues and Solutions
| Issue | Solution |
|---|---|
'java' is not recognized | Add JDK bin to PATH |
JAVA_HOME not set | Set JAVA_HOME to JDK root (not bin) |
| Permission denied (Linux) | Use sudo for system-wide installs |
| Conflicting Java versions | Use update-alternatives (Linux) or SDKMAN! |
| macOS security warning | Go to System Settings → Privacy & Security and allow the app |
9. Recommended Development Tools
After installing Java, consider these tools:
- IDE: IntelliJ IDEA, Eclipse, or VS Code with Java extensions
- Build Tools: Maven or Gradle
- Package Manager: SDKMAN! (for managing Java versions)
Conclusion
You now have a fully functional Java development environment! By installing the JDK and configuring your system correctly, you’re ready to write, compile, and run Java applications. Remember to:
- Use OpenJDK for a free, open-source solution.
- Set
JAVA_HOMEfor compatibility with build tools. - Keep your JDK updated for security and performance.
Whether you’re learning Java, building a personal project, or developing enterprise software, this setup provides a solid foundation. Happy coding! 🚀