Setting Up Java on Windows, Linux, and macOS
Java is a powerful programming language used for building applications, web services, and more. To start developing with Java, you need to set up the Java Development Kit (JDK) and configure the JAVA_HOME
environment variable. This guide will walk you through setting up Java on Windows, Linux, and macOS.
Installing Java
Windows
- Download the JDK
- Visit the Oracle JDK website and download the latest JDK.
- Choose the appropriate installer (e.g., Windows x64 Installer
.exe
). - Run the installer and follow the setup instructions.
- Verify Installation Open Command Prompt (
Win + R
→cmd
) and run:java -version
You should see the installed Java version. - Set JAVA_HOME
- Open System Properties (
Win + R
→sysdm.cpl
→ Advanced → Environment Variables). - Under “System Variables,” click “New” and enter:
- Variable name:
JAVA_HOME
- Variable value:
C:\Program Files\Java\jdk-XX.X.X
(replace with your actual installation path)
- Variable name:
- Add
%JAVA_HOME%\bin
to thePath
variable.
- Open System Properties (
- Verify Configuration Open a new Command Prompt and run:
echo %JAVA_HOME% javac -version
If correctly set up, you should see the Java installation path and the compiler version.
Linux (Ubuntu/Debian)
- Install OpenJDK Run the following command in the terminal:
sudo apt update sudo apt install openjdk-17-jdk
- Verify Installation
java -version
- Set JAVA_HOME Find the Java installation path:
sudo update-alternatives --config java
It will display a list of installed Java versions.Edit the profile settings:nano ~/.bashrc
Add the following lines at the end:export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 export PATH=$JAVA_HOME/bin:$PATH
Save and apply changes:source ~/.bashrc
- Verify Configuration
echo $JAVA_HOME javac -version
macOS
- Install Java via Homebrew
brew install openjdk@17
- Find Java Installation Path
/usr/libexec/java_home -V
- Set JAVA_HOME Edit your shell configuration file (for
zsh
users):nano ~/.zshrc
Add the following line:export JAVA_HOME=$(/usr/libexec/java_home) export PATH=$JAVA_HOME/bin:$PATH
Save and apply changes:source ~/.zshrc
- Verify Configuration
echo $JAVA_HOME javac -version