Java Installation and Environment Setup Step by Step

Java Installation & Environment Setup

Before writing and running any Java program, Java must be properly installed and configured on your system. A correct installation ensures that Java programs compile without errors and execute reliably across different environments. This post explains Java installation and environment setup step by step, covering Windows, macOS, and Linux, with clear explanations of why each step is required.


What Needs to Be Installed for Java

To work with Java, you must install the Java Development Kit (JDK).
The JDK includes:

Installing only JRE is not sufficient for development.


Checking If Java Is Already Installed

Before installing Java, check whether it is already available on your system.

Command

java -version

If Java is installed, the command will display the installed Java version.
If not, the system will indicate that Java is not recognized.

Image

Downloading the JDK

Java should always be installed from an official and trusted source.

Key points:

Image
Image

Java Installation on Windows

Step-by-step installation

  1. Download the Windows JDK installer (.exe)
  2. Run the installer
  3. Follow the installation wizard
  4. Choose the default installation path (recommended)
  5. Complete the installation

Once installed, Java binaries are placed inside the JDK directory.


Setting Environment Variables on Windows

Environment variables allow the system to locate Java tools from any directory.

Required variables

Steps

  1. Open System Properties
  2. Go to Environment Variables
  3. Add a new system variable:
    • Name: JAVA_HOME
    • Value: JDK installation path
  4. Edit the Path variable and add:%JAVA_HOME%\bin

Restart the terminal after setting variables.


Java Installation on macOS

Installation steps

  1. Download the macOS JDK installer (.dmg)
  2. Run the installer package
  3. Complete the setup using default options

macOS installs Java in a standard directory automatically.


Setting Environment Variables on macOS

macOS uses shell configuration files.

Command

nano ~/.zshrc

Add:

export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH

Save the file and reload:

source ~/.zshrc

Java Installation on Linux

Using package manager (recommended)

sudo apt update
sudo apt install openjdk-17-jdk

Verify installation

java -version

Setting Environment Variables on Linux

Edit the profile file:

nano ~/.bashrc

Add:

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH

Reload the file:

source ~/.bashrc
Image

Verifying Java Installation (All OS)

After installation and setup, verify Java:

java -version
javac -version

Both commands should return version information.


Writing and Running First Java Program

Create a file

class TestJava {
    public static void main(String[] args) {
        System.out.println("Java setup successful");
    }
}

Compile

javac TestJava.java

Run

java TestJava

Output:

Java setup successful

Common Installation Issues and Fixes

Java not recognized

Multiple Java versions

Permission issues (Linux/macOS)


Why Environment Setup Matters

Correct environment setup ensures:

Incorrect setup leads to compilation failures and runtime errors.


Conclusion

Java installation and environment setup is the foundation of Java development. Installing the correct JDK version and configuring environment variables ensures smooth compilation and execution of Java programs. Once Java is set up properly, developers can focus entirely on writing and maintaining reliable Java applications without system-level issues.

🤖
PrepCampusPlus AI Tutor
Scroll to Top