Chapter 5: How to Set Up Java Path

  • 4.1/5
  • 9
  • Mar 11, 2025
When working with Java, setting up the environment correctly is crucial to running Java programs. One of the key steps is setting the Java Path so that the system recognizes Java commands like javac and java globally.

Why Do We Need to Set the Java Path?

By default, Java binaries (like javac and java) reside in the JDK installation directory. Without setting the Java Path, you must manually navigate to the Java installation folder each time to run Java commands.

Setting the Path variable ensures that Java commands work globally from any directory in the command prompt or terminal.

Setting Java Path in Windows

1) Temporary Path Setup (For Current Session Only)

This method sets the Java path temporarily for the current command prompt session.

1. Open Command Prompt (cmd).

2. Find your Java installation path (e.g., C:\Program Files\Java\jdk-XX.X.X\bin).

3. Type the following command and press Enter:
set PATH=C:\Program Files\Java\jdk-XX.X.X\bin;%PATH%
4. Verify by running:
java -version
javac -version
Note: This setting will be lost after closing the command prompt.

2) Permanent Path Setup (For All Sessions)

To permanently set the Java Path on Windows:

1. Find Java Installation Directory

- Open C:\Program Files\Java - Note the JDK version folder (e.g., jdk-XX.X.X)

2. Set Environment Variable - Open Control Panel → Search for Environment Variablesbr /> - Click on Edit the system environment variablesbr /> - Under System Properties, click Environment Variables

3. Update PATH Variable - Under System Variables, find Path and click Editbr /> - Click New, then paste the JDK bin path:
C:\Program Files\Java\jdk-XX.X.X\bin
- Click OK to save changes

4. Verify Setup Open Command Prompt and check Java version:
java -version
javac -version


Setting Java Path in Linux OS

1) Temporary Path Setup (For Current Session Only)

Open the terminal and type:
export PATH=/usr/lib/jvm/java-XX-openjdk-amd64/bin:$PATH
Replace java-XX-openjdk-amd64 with your actual JDK installation path.

Verify by running:
java -version
javac -version
Note: This setting will be lost after closing the terminal.

2) Permanent Path Setup

To make the Java Path persistent across reboots:

1. Open the Terminal.
2. Edit the ~/.bashrc (for Bash shell) or ~/.bash_profile:
nano ~/.bashrc
3. Add the following lines at the end:
export JAVA_HOME=/usr/lib/jvm/java-XX-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
4. Save and exit (CTRL + X, then Y and Enter).

5. Apply changes:
export JAVA_HOME=/usr/lib/jvm/java-XX-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
Verify using:
java -version
javac -version
For Zsh users: Modify ~/.zshrc instead of ~/.bashrc.

Setting Java Path in macOS

1) Temporary Path Setup

To set the Java path temporarily, use:
export PATH=/Library/Java/JavaVirtualMachines/jdk-XX.X.X.jdk/Contents/Home/bin:$PATH
Verify:
java -version
javac -version
This setting will be lost after restarting the terminal.

2) Permanent Path Setup

To make it permanent:

1. Open Terminal.
2. Edit the ~/.zshrc (for macOS Catalina and later) or ~/.bash_profile (for older versions):
nano ~/.zshrc
3. Add these lines:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-XX.X.X.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
4. Save and exit (CTRL + X, then Y and Enter).

5. Apply changes:
source ~/.zshrc
Verify:
java -version
javac -version
Setting the Java path correctly ensures that Java commands (javac, java) work seamlessly in Windows, Linux, and macOS. While temporary path setup works for a single session, configuring the path permanently saves time and effort.
Index
Modern Java - What’s new in Java 9 to Java 17

32 min

Chapter 1: Introduction

3 min

Chapter 2: History of Java

9 min

Chapter 3: Features of Java

4 min

Chapter 4: How to Install Java?

2 min

Chapter 5: How to Set Up Java Path

2 min

Chapter 6: JDK, JVM and JRE

2 min

Chapter 7: First Java Program in Java

15 min

What is ClassLoader in Java ?

2 min

Object Oriented Programming (OOPs) Concept

17 min

Concurrency in Java: Creating and Starting a Thread

12 min

Concurrency in Java: Interrupting and Joining Threads

5 min

Concurrency in Java: Race condition, critical section, and atomic operations

13 min

Concurrency in Java: Reentrant, Read/Write and Stamped Locks

11 min

Concurrency in Java: "synchronized" and "volatile" keywords

10 min

Concurrency in Java: using wait(), notify() and notifyAll()

6 min

Concurrency in Java: What is "Semaphore" and its use?

2 min

Concurrency in Java: CompletableFuture and its use

18 min

Concurrency in Java: Producer-consumer problem using BlockingQueue

2 min

Concurrency in Java: Producer-Consumer Problem

2 min

Concurrency in Java: Thread pools, ExecutorService & Future

14 min

Java 8 Lambdas, Functional Interface & "static" and "default" methods

28 min

Method Reference in Java (Instance, Static, and Constructor Reference)

9 min

What's new in Java 21: A Tour of its Most Exciting Features

14 min

Java Memory Leaks & Heap Dumps (Capturing & Analysis)

9 min

Memory footprint of the JVM (Heap & Non-Heap Memory)

15 min