Setting up Environment Variables For Java - Complete Guide to Set JAVA_HOME
In the journey to learning the Java programming language, setting up environment variables for Java is essential because it helps the system locate the Java tools needed to run the Java programs. Now, this guide on how to setting up environment variables for Java is a one-place solution for Mac, Windows, and Linux users. Here we have discussed the step-by-step process to set the Environment variable in all three major OS.
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 JDK
- 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: Update the Path Variable
- 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: Check the Java Version
- 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 for Java in Linux
If you are Linux user then find the complete solution to setting up environment variables for Java on Linux operating system.
Step 1: Downlaod and Install JAVA JDK
After installing the JDK in Linux, We need to configure the system so that it can recognize Java.
Step 2: Open Linux Terminal
- 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 Path
- Update the PATH Variable on Linux
- Add the JDK
bin
directory to thePATH
, and run the below command in the terminal:
export PATH=$PATH:$JAVA_HOME/bin
Step 4: Change Environment Variable Permanent
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.
Step 6: Check Updated Path
- 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 for Java in macOS
If you are Mac user then find the complete solution to setting up environment variables for Java on macOS operating system.
Step 1: Configure the System
- After installing the JDK in macOS, we need to configure the system so that it can recognize Java.
Step 2: Set up the Java Home
- Now to set the
JAVA_HOME
andPATH
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: Change the Base
- 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: Varify an Installation
- 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.
Step 5: Run the Compiled Java Program
After compiling the Java program, we can run it using the following command:
java HelloWorld
Why Don't We Include .class in the Command?
When running a Java program, the java command expects a class name. It doesn't matter what the file name is, as long as the class with the main() method is specified.
For example: The correct command is java HelloWorld. This will correctly run the program. The command java HelloWorld.class is an incorrect command, this will cause an error because the JVM is looking for a class named HelloWorld.class, not HelloWorld.
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.
Conclusion
Creating a smooth and efficient development workflow requires setting up environment variables for Java. By helping your system locate essential tools like the Java compiler and runtime, these variables simplify many tasks, such as compiling and executing Java programs. A properly configured variable like JAVA_HOME also ensures seamless integration of Java with other frameworks and development tools. Whether you use Windows, macOS, or Linux, you can customize your Java environment to meet your specific needs by following the steps outlined in this guide