How to Set JAVA_HOME for JDK & JRE: A Step-by-Step Guide

Download Article
The easiest ways to set the JAVA_HOME environment variable on any computer
Download Article

Are you seeing Java errors like "JAVA_HOME is not defined correctly?" or "JAVA_HOME is set to an invalid directory?" If you've recently installed the Java Development Kit (JDK) or the Java Runtime Environment (JRE), you'll need to set your JAVA_HOME variables and configure the path so applications know where to find Java. This How.com.vn article will show you the easiest ways to change or set the Java home path on Windows, macOS, and Linux.

Things You Should Know

  • Before you can set JAVA_HOME, you'll need the full path to your JDK or JRE installation.
  • Once you set the JAVA_HOME environment variable, you can run the command echo $JAVA_HOME to see the new path.
  • To set the Java home and path on Linux or macOS permanently (even after a reboot), add the environment variables to your .bashrc or .zshrc file.
Method 1
Method 1 of 3:

Windows

Download Article
  1. How.com.vn English: Step 1 Find your Java installation directory.
    Assuming that you didn't change the path for the JDK or JRE during installation is not changed, it should be in a directory under C:\Program Files\Java. There are a few ways to find the full path:
    • Open File Explorer, click This PC in the left panel, then navigate to C:\Program Files\Java. The directory you're looking for should have the name of the JDK version, such as C:\Program Files\Java\jdk-19.
    • If you installed the JRE instead of the JDK, you'll have something like C:\Program Files\Java\jre1.8.0_351 instead.
    • You can also open the command prompt and run the command wmic product where "Name like '%%Java%%'" get installlocation,Name. This will tell you the full path of the JDK, even if you haven't yet set JAVA_HOME.
  2. How.com.vn English: Step 2 Open your Advanced System Settings.
    Here's how:
    • Press the Windows key on your keyboard and type advanced system.
    • Click View advanced system settings in the search results.
    Advertisement
  3. How.com.vn English: Step 3 Click the Environment Variables button.
    You'll see it at the bottom-right corner of the System Properties window.
  4. Step 4 Click the New… button under "User variables."
    You'll have two sections on this window—"User variables" and "System variables." You'll want to make sure you're clicking New under the first box (User variables).
    • If you have multiple installations of the JDK and want to change JAVA_HOME to your new installation, select the current JAVA_HOME user variable and click Edit… instead.
  5. Step 5 Type JAVA_HOME as the "Variable name."
    • If you're editing the current JAVA_HOME path, you'll already have JAVA_HOME here. So, you can skip this step.
  6. How.com.vn English: Step 6 Enter the full path to your JDK or JRE installation.
    For example, if the path is C:\Program Files\Java\jdk-19, enter that full path into the "Variable value" field.[1]
    • If you're adding a second path to JAVA_HOME, just type a semicolon (;) after the first path, then enter the second path.
    • If you're replacing an old JAVA_HOME path, just delete the current path and enter the new one.
  7. How.com.vn English: Step 7 Click OK.
    This takes you back to the Environment Variables window.
  8. How.com.vn English: Step 8 Select the Path user variable and click Edit.
    You should already have a variable in the "User variables" section called "Path." To make sure you can run the Java binaries, you'll need to edit your path to add the location of the JDK bin directory.
    • If you want other users on this PC to be able to access Java binaries from the command line, repeat this step for the "Path" variable under "System variables" as well.
  9. How.com.vn English: Step 9 Append the path to your Java /bin directory to the end of the path.
    You'll already have at least one directory in your path, which needs to stay in place. Here's how to add the Java bin directory:
    • Click the New button at the top.
    • Enter the full path to the JRE or JDK with \bin at the end. For example, C:\Program Files\Java\jdk-19\bin or C:\Program Files\Java\jre1.8.0_351\bin.
    • Click OK.
  10. How.com.vn English: Step 10 Click OK on all subsequent windows.
    This updates your JAVA_HOME variable and adds the Java binaries to your path.
    • You won't need to restart your computer for the changes to take effect, but you will need to relaunch any apps that were trying to access Java.
    • Open a new command prompt window and run the command echo %JAVA_HOME% to display the new path to JAVA_HOME.
      • Make sure this is a new command prompt window. If you still have the same window open, the command will fail because it doesn't have the new environment variables.
  11. Advertisement
Method 2
Method 2 of 3:

macOS

Download Article
  1. How.com.vn English: Step 1 Open a Terminal window.
    To open Terminal, search for Terminal in Spotlight. Or, you can open Finder, click the Go menu, select Utilities, and choose the Terminal app.
  2. How.com.vn English: Step 2 Type /usr/libexec/java_home and press ⏎ Return.
    This handy utility that comes preinstalled on macOS will tell you the full path to your Java installation, which you'll need to set JAVA_HOME.
    • If you have more than one Java installation and want to see the paths to all of them, use /usr/libexec/java_home -V instead.
  3. How.com.vn English: Step 3 Copy the path and paste it somewhere handy.
    Highlight the path to the Java installation you want to use as JAVA_HOME, press Cmd + V to copy it, then paste it into a sticky note or text file.
  4. How.com.vn English: Step 4 Open your .zshrc file in a text editor.
    As long as you're using macOS Catalina or later, you'll be using the zsh shell by default, which means you'll add JAVA_HOME and the path to your .zshrc file.[2] If you're using an earlier version of macOS (or prefer the bash shell), replace .zshrc with .bashrc in these commands instead. Here's the easiest way to open the file:
    • Type cd ~ and press Return.
    • Type open .zshrc and press Return. This should open the file in a text editor.
    • If the file is not found, type echo > .zshrc and press Return. Then, run open .zshrc again.
  5. How.com.vn English: Step 5 Add these lines to the bottom of the file.
    Adding these lines ensures that JAVA_HOME will be set correctly even after you log off or open another window. If the file has existing contents, just scroll down past them to enter the following text on individual lines:
    • export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home
      • Replace /Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home with the full path to the /Contents/Home directory of your Java installation if it's different.
    • export PATH=$JAVA_HOME/bin:$PATH
    • Save the file and exit the text editor when you're finished.
  6. How.com.vn English: Step 6 Type source .zshrc and press ⏎ Return.
    Once you've edited your profile, this command ensures that your environment variables will be updated for the current terminal window (and any other windows you open from now on).
  7. How.com.vn English: Step 7 Type echo $JAVA_HOME and press ⏎ Return to confirm JAVA_HOME.
    You should now see the full path to JAVA_HOME.
    • If you had any other windows open that were attempting to find Java binaries, close and reopen them.
  8. Advertisement
Method 3
Method 3 of 3:

Linux

Download Article
  1. How.com.vn English: Step 1 Open a terminal window.
    If you're logged in to your desktop environment, you can press Ctrl + Alt + T to launch a terminal window.
    • This method will work in just about any version of Linux, including Ubuntu and Redhat.
  2. How.com.vn English: Step 2 Find the location of your Java installation.
    One way you might find the Java install location is to run the which javac command, but this will only give you a symbolic link. Instead, run this command to find the location of the executable file:
    • readlink -f `which javac`
    • If that doesn't work, try running update-alternatives --list java.
    • If neither of these commands works, run whereis java, which will usually result in a symbolic link like /usr/bin/java.
      • Once you get the directory, find out where it links using ls -la /bin/java.
      • If that points you to another directory, e.g., /etc/alternatives/java, run ls -la /etc/alternatives/java.
      • At that point, you should see a much longer directory, which is actually the home to the Java binaries. For example, usr/lib/jvm/java-11-openjdk-arm64/bin/java. This is the directory you want.
  3. How.com.vn English: Step 3 Set the JAVA_HOME path.
    Now that you have the location of Java, you can add the path to your .bashrc file (if you're using the bash shell), .zshrc (for the zsh shell), or the equivalent for your shell.[3] Run both of the following commands, replacing the path with the path to your JDK or JRE installation, and "bashrc" with your shell's login script:
    • echo "export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-arm64" >> ~/.bashrc
    • echo "export PATH=$PATH:$JAVA_HOME/bin" >> ~/.bashrc
  4. How.com.vn English: Step 4 Type source .bashrc to apply the changes to the current terminal window.
    Though you've already set the home for Java and added the path to its binaries permanently, the changes won't take effect in your current terminal window until you run this command.
    • At this point, you can run echo $JAVA_HOME to see the location of Java on your system.
  5. Advertisement

Expert Q&A

Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Video

      Tips

      • In Linux, you can set JAVA__HOME for all users by editing the global .bashrc, which is located at /etc/bash.bashrc. Just use echo and replace ~/.bashrc with /etc/bash.bashrc.
      • On both Linux and macOS, you can set the Java home in your .bash_profile or .zprofile if you prefer.
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

      About This Article

      How.com.vn English: Nicole Levine, MFA
      Written by:
      How.com.vn Technology Writer
      This article was co-authored by How.com.vn staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for How.com.vn. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions. This article has been viewed 359,050 times.
      How helpful is this?
      Co-authors: 12
      Updated: September 27, 2023
      Views: 359,050
      Categories: Java
      Thanks to all authors for creating a page that has been read 359,050 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement