Setting up Environment Variables For Java
Setting up environment variables for Java is essential because it helps the system locate the Java tools needed to run the Java programs. Before setting up the environment variables, the Java Development Kit (JDK) needs to be installed on your system and you must know the JDK installation directory on your system.
To set Java Environment Variables, you need to set,
- JAVA_HOME: It points to the directory where the JDK is installed on the system.
- PATH: It specifies where the operating system should look for executable files.
Setting the Environment Variables in Windows
Step 1: Download and install the latest JDK from the official Oracle website. To know how to download JDK on Windows OS, refer to this article: How to Install Java on Windows, Linux and macOS?
Step 2: Set JAVA_HOME
- Click Windows + S, search for “Environment Variables”, and click Edit the system environment variables. In the System Properties window, click Environment Variables.
- Now, go to Environment Variable > System Variable section > click New.
In the Variable name field, enter JAVA_HOME and in the Variable value field enter the path where the JDK is installed. Click OK to save the changes.
Step 3: Now we need to update the PATH variable. For this, open the C: drive, go to Program Files > Java > jdk-23 > bin folder, then copy the path.
Go inside System Variables section, select the path variable. Right-click on it and choose the option Edit. In the Edit environment window, click New.
Then add the path to the JDK’s bin folder (e.g., C:\Program Files\Java\jdk-23\bin).
Click OK to save the changes.
Step 4: Now to verify the installation, open command Prompt and run the below commands:
java –version
To verify the Java compiler, run the below command:
Javac -version
Setting up Environment Variables in Linux
Step 1: After installing the JDK in Linux, We need to configure the system so that it can recognize Java.
Step 2: Now we need to set JAVA_HOME. To do this,
Open the terminal and run the below command:
export JAVA_HOME=/path/to/your/jdk
Step 3: Update the PATH Variable on Linux
Add the JDK bin directory to the PATH, and run the below command in the terminal:
export PATH=$PATH:$JAVA_HOME/bin
Step 4: The above changes are temporary. To make the environment variable changes permanent, you need to add the export commands to your shell’s configuration file. Depending on the shell you are using.
- For bash use the command nano ~/.bashrc
- For zsh use the command nano ~/.zshrc
- For fish use the command nano ~/.config/fish/config.fish
Add the following commands at the end of the file.
export JAVA_HOME=/path/to/your/jdk
export PATH=$PATH:$JAVA_HOME/bin
After saving the file, run the following command to apply the changes:
source ~/.bashrc
This will set your JAVA_HOME and PATH variables on Linux permanently.
Step 5: Verify the Installation
To confirm the JAVA_HOME path, run the below command:
echo $JAVA_HOME
It should output the path of JDK Installer. If the output is empty that means the JAVA_HOME environment variable is not set properly.
To confirm the updated PATH, run the below command:
echo $PATH
It should output the path of the bin directory to your JDK. If the output is empty that means the PATH environment variable is not set properly. Then write java --version and javac -version to verify the installation.
Setting up Environment Variables in macOS
Step 1: After installing the JDK in macOS, we need to configure the system so that it can recognize Java.
Step 2: Now to set the JAVA_HOME and PATH environment variables permanently, we need to add them to our shell’s configuration file depending on the shell we are using.
- For bash (if you have older version of macOS) run the command nano ~/.bash_profile
- For zsh(default shell for macOS 10.15 Catalina and later) run the command nano ~/.zshrc
Add the following commands at the end of the file:
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
Step 3: Now, we need to apply the changes.
For bash apply the changes by running the below command:
source ~/.bash_profile
For zsh apply the changes by running the below command:
source ~/.zshrc
Step 4: Verify the Installation
Check JAVA_HOME environment variable by running the below command:
echo $JAVA_HOME
It should output the path of JDK Installer. If the output is empty that means the JAVA_HOME environment variable is not set properly.
Check PATH environment variable by running the below command:
echo $PATH
It should output the path of the bin directory to your JDK. If the output is empty that means the PATH environment variable is not set properly.
Then write java --version and javac -version command to verify the installation on macOS.
After following these steps, your environment variable for Java will be set correctly, and you can easily run Java programs from the terminal without any issues.
Start your Java programming journey today with our Java Programming Online Course, designed for both beginners and advanced learners. With self-paced lessons covering everything from basic syntax to advanced concepts, you’ll gain the skills needed to excel in the world of programming.
Take the Three 90 Challenge! Complete 90% of the course in 90 days, and earn a 90% refund. Track your progress and stay motivated as you master Java.
Join now and start your path to becoming a Java expert!


