Java Installation and Setup Tutorial for Beginners
Last Updated on: 15th Aug 2025 18:06:13 PM
This tutorial is designed for beginner students to set up a Java development environment step-by-step. We’ll cover downloading and installing the Java Development Kit (JDK), setting the JAVA_HOME environment variable, installing and using an Integrated Development Environment (IDE), writing your first Java program (Hello World), and compiling and running it using the command line. Each step is explained in a simple, beginner-friendly way with clear instructions for Windows, Linux, and Mac.
1. Download and Install JDK
The Java Development Kit (JDK) is a software package that includes tools to write, compile, and run Java programs. It contains the Java Virtual Machine (JVM), the Java compiler (javac), and standard libraries.
Step 1: Download the JDK
-
Visit the official Oracle JDK download page or the OpenJDK website.
-
Choose the latest version (e.g., JDK 21 as of 2025) for your operating system (Windows, Linux, or macOS).
-
Select the appropriate installer:
-
Windows: Download the .exe file.
-
Linux: Download the .tar.gz or .deb file (depending on your distribution).
-
macOS: Download the .dmg file.
-
-
Click the download link and accept any license agreements if prompted.
Step 2: Install the JDK
-
Double-click the downloaded .exe file to start the installer.
-
Follow the setup wizard:
-
Choose the installation directory (default is fine, e.g., C:Program FilesJavajdk-21).
-
Click Next through prompts and then Install.
-
-
Wait for the installation to complete and click Finish.
-
For Ubuntu/Debian (using .deb file):
-
Open a terminal and navigate to the download folder: cd ~/Downloads.
-
Run: sudo dpkg -i <jdk-file-name>.deb (replace <jdk-file-name> with the actual file name).
-
If dependencies are missing, run: sudo apt-get install -f.
-
-
For other distributions (using .tar.gz):
-
Extract the file: tar -xvzf <jdk-file-name>.tar.gz.
-
Move it to a system directory: sudo mv jdk-21 /usr/lib/jvm/.
-
-
Verify installation: java -version.
-
Double-click the downloaded .dmg file.
-
Follow the installer prompts to install the JDK.
-
The JDK will typically be installed in /Library/Java/JavaVirtualMachines/jdk-21.jdk/.
-
Verify installation: Open a terminal and type java -version.
Step 3: Verify JDK Installation
-
Open a terminal (Command Prompt on Windows, Terminal on Linux/macOS).
-
Type java -version and press Enter.
-
You should see output like:
java version "21.0.1" 2025-01-14
Java(TM) SE Runtime Environment (build 21.0.1+12-29)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.1+12-29, mixed mode, sharing)
-
If you see this, the JDK is installed correctly. If not, recheck the installation steps.
2. Setting JAVA_HOME Environment Variable
The JAVA_HOME environment variable tells your system where the JDK is installed. Some tools and IDEs need this to work properly.
-
Find the JDK Path:
-
The default path is usually C:Program FilesJavajdk-21.
-
If you installed it elsewhere, locate the folder.
-
-
Set JAVA_HOME:
-
Right-click This PC (or My Computer) and select Properties.
-
Click Advanced system settings > Environment Variables.
-
Under System Variables, click New.
-
Set:
-
Variable name: JAVA_HOME
-
Variable value: C:Program FilesJavajdk-21 (your JDK path).
-
-
Click OK.
-
-
Update PATH:
-
In System Variables, find and select the Path variable, then click Edit.
-
Add a new entry: %JAVA_HOME%in.
-
Click OK to save all changes.
-
-
Verify:
-
Open a new Command Prompt and type echo %JAVA_HOME%.
-
It should display the JDK path.
-
Type javac -version to confirm the compiler is accessible.
-
-
Find the JDK Path:
-
Typically /usr/lib/jvm/jdk-21 (for .tar.gz) or check with update-alternatives --config java.
-
-
Set JAVA_HOME:
-
Open the terminal and edit the profile file: nano ~/.bashrc (or ~/.zshrc for Zsh).
-
Add at the end:
-
export JAVA_HOME=/usr/lib/jvm/jdk-21
export PATH=$JAVA_HOME/bin:$PATH
-
Save (Ctrl+O, Enter, Ctrl+X in nano) and run: source ~/.bashrc.
-
Verify:
-
Type echo $JAVA_HOME to see the path.
-
Type javac -version to confirm.
-
-
Find the JDK Path:
-
Run /usr/libexec/java_home in the terminal to find the path (e.g., /Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home).
-
-
Set JAVA_HOME:
-
Open the terminal and edit the profile file: nano ~/.zshrc (or ~/.bash_profile for older macOS).
-
Add:
-
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
-
Save and run: source ~/.zshrc (or ~/.bash_profile).
-
Verify:
-
Type echo $JAVA_HOME to see the path.
-
Type javac -version to confirm.
-
3. Installing and Using an IDE
An Integrated Development Environment (IDE) makes coding easier with features like code completion, debugging, and project management. We’ll cover three popular IDEs: Eclipse, IntelliJ IDEA, and Visual Studio Code.
Option 1: Eclipse
-
Download:
-
Visit Eclipse Downloads.
-
Choose Eclipse IDE for Java Developers and download the installer for your OS.
-
-
Install:
-
Run the installer and select Eclipse IDE for Java Developers.
-
Choose an installation directory and complete the setup.
-
-
Setup:
-
Open Eclipse and select a workspace (a folder to store your projects).
-
Create a new Java project: File > New > Java Project.
-
Name your project (e.g., MyFirstProject) and click Finish.
-
-
Verify:
-
Ensure Eclipse detects your JDK (it should prompt you to select a JDK if not set).
-
Option 2: IntelliJ IDEA
-
Download:
-
Visit JetBrains IntelliJ IDEA.
-
Download the Community Edition (free) for your OS.
-
-
Install:
-
Run the installer and follow the prompts.
-
Accept the default settings for simplicity.
-
-
Setup:
-
Open IntelliJ and create a new project: New Project > Java.
-
Select your JDK (it should detect JAVA_HOME).
-
Name your project and click Create.
-
-
Verify:
-
Check that IntelliJ runs a sample program without errors.
-
Option 3: Visual Studio Code
-
Download:
-
Visit Visual Studio Code and download for your OS.
-
-
Install:
-
Run the installer and follow the prompts.
-
-
Setup for Java:
-
Open VS Code and go to Extensions (Ctrl+Shift+X or Cmd+Shift+X).
-
Search for Java Extension Pack by Microsoft and install it.
-
Restart VS Code if prompted.
-
-
Create a Project:
-
Create a new folder (e.g., JavaProjects).
-
Open it in VS Code: File > Open Folder.
-
Create a new Java file (e.g., HelloWorld.java) via File > New File.
-
-
Verify:
-
Ensure VS Code recognizes the JDK (it will prompt if not set).
-
Recommendation for Beginners: IntelliJ IDEA Community Edition is the most user-friendly, but Eclipse is lightweight, and VS Code is great if you’re familiar with it for other languages.
4. Writing Your First Java Program (Hello World)
Let’s create a simple Hello World program to print a message to the console.
Step-by-Step
-
Open Your IDE:
-
In Eclipse: Create a new Java project, then add a new class (e.g., HelloWorld).
-
In IntelliJ: Create a new Java project and add a class.
-
In VS Code: Create a new file named HelloWorld.java.
-
-
Write the Code:
-
Copy the following code into HelloWorld.java:
-
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}