Of course! Setting the JAVA_HOME environment variable is a crucial step for many Java-based applications and build tools (like Maven, Gradle, and Tomcat) to locate your Java Development Kit (JDK).

The process is different depending on your operating system. Follow the instructions for your OS below.
Part 1: Find Your Java Installation Path
Before you can set JAVA_HOME, you need to know where the JDK is installed. Open a terminal or command prompt and run the following command:
java -XshowSettings:properties -version 2>&1 | grep "java.home"
This command will output the path to your Java installation. The JAVA_HOME variable should point to the parent directory of the bin folder.
For example, if the output is:
java.home = /Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home

Then your JAVA_HOME should be:
/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
If the output is:
java.home = C:\Program Files\Java\jdk-17.0.2
Then your JAVA_HOME should be:
C:\Program Files\Java\jdk-17.0.2
Part 2: Set the Environment Variable
Now, use the path you found to set the variable.

On Windows
There are two common methods: using the GUI or using the Command Prompt.
Method 1: Using the System Properties GUI (Recommended for most users)
- Press the Windows Key, type
env, and select "Edit the system environment variables". - In the System Properties window, click the "Environment Variables..." button.
- In the "System variables" section (at the bottom), click "New...".
- Enter the following information:
- Variable name:
JAVA_HOME - Variable value: (Paste the path you found in Part 1, e.g.,
C:\Program Files\Java\jdk-17.0.2)
- Variable name:
- Click OK on all windows to save the changes.
Method 2: Using Command Prompt (Admin)
This method is useful for scripting or if you don't have GUI access.
-
Open Command Prompt as Administrator. Right-click on the Start Menu and select "Windows PowerShell (Admin)" or "Command Prompt (Admin)".
-
Find your JDK path (if you don't have it already) using the command from Part 1.
-
Set the variable. Replace
"C:\Path\To\Your\JDK"with your actual path.setx JAVA_HOME "C:\Program Files\Java\jdk-17.0.2"
Note:
setxmakes the variable permanent for future command prompt sessions, but it won't affect the current one. You may need to open a new Command Prompt for the change to take effect.
(Optional) Update the PATH Variable
Some older applications might still rely on the PATH variable. It's generally better to use JAVA_HOME, but if you need to update PATH:
- In the "Environment Variables" window (from Method 1), find the
Pathvariable in the "System variables" list and click "Edit...". - Click "New" and add the path to your JDK's
bindirectory. For our example, this would be:C:\Program Files\Java\jdk-17.0.2\bin. - Move this new entry to the top of the list to ensure it's found first.
- Click OK on all windows.
On macOS
Method 1: Using ~/.zshrc (for macOS Catalina and later)
The default shell on modern macOS is Zsh.
-
Open Terminal.
-
Find your JDK path using the command from Part 1.
-
Edit the configuration file. Open the
~/.zshrcfile in a text editor like Nano:nano ~/.zshrc
-
Add the following lines to the end of the file. Replace the path with your own.
# Set JAVA_HOME export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
-
Save the file and exit the editor (in Nano, press
Ctrl+X, thenY, thenEnter). -
Apply the changes to your current terminal session by running:
source ~/.zshrc
Method 2: Using ~/.bash_profile (for older macOS versions or Bash shell)
If you are using the older Bash shell, you would edit ~/.bash_profile instead.
- Open Terminal.
- Edit the configuration file:
nano ~/.bash_profile
- Add the following lines, replacing the path:
# Set JAVA_HOME export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
- Save and exit.
- Apply the changes:
source ~/.bash_profile
On Linux (Ubuntu, Debian, CentOS, etc.)
The process is similar to macOS, but the shell configuration file is usually ~/.bashrc.
-
Open Terminal.
-
Find your JDK path using the command from Part 1. On Linux, it's often something like
/usr/lib/jvm/java-17-openjdk-amd64. -
Edit the configuration file:
nano ~/.bashrc
-
Add the following lines to the end of the file. Replace the path with your own.
# Set JAVA_HOME export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
-
Save and exit the editor.
-
Apply the changes to your current terminal session:
source ~/.bashrc
How to Verify Your Changes
After setting the variable, it's important to verify that it's working correctly.
- Open a new terminal window to ensure you are in a clean session.
- Check the value of
JAVA_HOME:- Windows:
echo %JAVA_HOME% - macOS / Linux:
echo $JAVA_HOME
- Windows:
- Check the Java version using the
JAVA_HOMEpath:- Windows:
%JAVA_HOME%\bin\java -version - macOS / Linux:
$JAVA_HOME/bin/java -version
- Windows:
You should see output similar to this, confirming that Java is being found from the correct location:
openjdk version "17.0.2" 2025-01-18
OpenJDK Runtime Environment Temurin-17.0.2+8 (build 17.0.2+8)
OpenJDK 64-Bit Server VM Temurin-17.0.2+8 (build 17.0.2+8, mixed mode, sharing) 