how to add java to path

Mastering Java PATH: A Complete Setup Guide

By Robert Martinez
AD

When you're working on Java projects, it's crucial to ensure that you can seamlessly compile and run your Java programs. One key aspect of this is correctly setting the JAVA_HOME and path environment variables. This guide will walk you through the process of adding Java to your system's path, ensuring a smoother development experience.

Before we dive in, make sure you have Java Development Kit (JDK) installed on your system. If not, download and install the JDK from the official Oracle website. Once you've installed the JDK, let's proceed to add Java to your system's path.

Setting the JAVA_HOME Variable

The JAVA_HOME environment variable points to the root directory of your JDK installation. Setting this variable is crucial for various tools and IDEs to locate your JDK. Here's how you can set it:

AD
🚀 Java Backend Roadmap 2026 | Complete learning path for Beginners & Developers
🚀 Java Backend Roadmap 2026 | Complete learning path for Beginners & Developers
Multithreading Learning Path 🧵 | Complete Java Concurrency Roadmap
Multithreading Learning Path 🧵 | Complete Java Concurrency Roadmap
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '

1. Identify the absolute path of your JDK directory. For instance, it might be /usr/lib/jvm/java-8-openjdk-amd64 for Ubuntu or C:\Program Files\Java\jdk1.8.0_261 for Windows.

Setting JAVA_HOME in Unix/Linux/MacOS

Open your terminal and create a new file named .bashrc (or .zshrc, depending on your shell) using a text editor like nano:

the roadmap to learn java from zero - to - job ready info sheet
the roadmap to learn java from zero - to - job ready info sheet
Java 8 Features Learning Path ⚡ | Master Modern Java
Java 8 Features Learning Path ⚡ | Master Modern Java
12 Java Project Ideas for Beginners to Advanced Developers | Build Your Portfolio
12 Java Project Ideas for Beginners to Advanced Developers | Build Your Portfolio

$ nano ~/.bashrc

Add the following line, replacing the path with your JDK's location:

AD

export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"

an info sheet with different types of web pages
an info sheet with different types of web pages
Javascript Roadmap
Javascript Roadmap
JAVA BEGINNER ROADMAP
JAVA BEGINNER ROADMAP
JAVA SPRING BOOT ROADMAP (2026)
JAVA SPRING BOOT ROADMAP (2026)
Complete Java Roadmap
Complete Java Roadmap
Java Learning Roadmap for Beginners | Step-by-Step Guide to Master Java
Java Learning Roadmap for Beginners | Step-by-Step Guide to Master Java

Save and exit the file, then reload it using the command source ~/.bashrc.

Setting JAVA_HOME in Windows

Search for 'Environment Variables' in your Windows search bar and select 'Edit the system environment variables'. Click on 'Environment Variables...' at the bottom of the window.

AD

Under 'System variables', find and select the 'Path' variable, then click 'Edit...'. In the new window, click 'New' and add the path to your JDK (e.g., C:\Program Files\Java\jdk1.8.0_261). Click 'OK' on all windows to save changes.

Java Roadmap: Beginner to Job Ready | Mini Project + Revision | Day 36
Java Roadmap: Beginner to Job Ready | Mini Project + Revision | Day 36
Java Basics Cheat Sheet | Complete Java Programming Guide for Beginners ☕
Java Basics Cheat Sheet | Complete Java Programming Guide for Beginners ☕
Java Developer Roadmap 2026 | Complete Core Java to Spring Boot Learning Guide for Beginners
Java Developer Roadmap 2026 | Complete Core Java to Spring Boot Learning Guide for Beginners

Now, let's add Java to the system's path to enable command-line execution.

Adding Java to the System's Path

Adding Java to your system's path allows you to use commands like javac, java, jarsigner, etc., directly from the terminal or command prompt without specifying the full path to the executable.

AD

Here's how to do it on different operating systems:

Adding Java to the Path in Unix/Linux/MacOS

Open your terminal and edit the .bashrc file (or .zshrc, depending on your shell) using a text editor like nano:

$ nano ~/.bashrc

Add the following lines at the end of the file, replacing the path with your JDK's location:

export PATH=$JAVA_HOME/bin:$PATH

Save and exit the file, then reload it using the command source ~/.bashrc.

Adding Java to the Path in Windows

Follow the same steps as setting the JAVA_HOME variable in Windows. In the 'Edit Environment Variable' window, under 'System variables', find and select the 'Path' variable, then click 'Edit...'.

In the new window titled 'Edit Environment Variable', click on 'New' and add the path to your Java's 'bin' directory (e.g., C:\Program Files\Java\jdk1.8.0_261\bin). Click 'OK' on all windows to save changes.

After completing these steps, you should be able to run Java commands directly from your terminal or command prompt. To verify, type:

java -version

You should see the installed Java version as output.

Adding Java to your system's path not only simplifies your development process but also enhances your productivity. Now that you've mastered this process, you can focus more on coding and less on configurations. Happy coding!

AD