Last Updated : 09 May, 2025
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
and in the Variable value field enter the path where the JDK is installed. Click OK to save the changes.C:\Program Files\Java\jdk-23\bin
).java --versionjava --version
Javac -version Setting up Environment Variables for Java in LinuxJavac -version
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 JDKAfter installing the JDK in Linux, We need to configure the system so that it can recognize Java.
Step 2: Open Linux TerminalStep 3: Update Pathexport JAVA_HOME=/path/to/your/jdk
bin
directory to the PATH
, and run the below command in the terminal: Step 4: Change Environment Variable Permanentexport PATH=$PATH:$JAVA_HOME/bin
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.
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.
JAVA_HOME
path, run the below command:echo $JAVA_HOME
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.
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 SystemJAVA_HOME
and PATH
environment variables permanently, we need to add them to our shell's configuration file depending on the shell we are using.Add the following commands at the end of the file:
Step 3: Change the Baseexport JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
source ~/.bash_profile
Step 4: Varify an Installationsource ~/.zshrc
echo $JAVA_HOME
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 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.
ConclusionCreating 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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4