- How to Set JAVA_HOME for JDK & JRE: A Step-by-Step Guide
- Windows
- macOS
- Linux
- Expert Q&A
- You Might Also Like
- About This Article
- Is this article up to date?
- Quizzes
- You Might Also Like
- How to Set JAVA_HOME Variable in Ubuntu Linux Correctly
- Step 1: Check if JDK is installed
- Step 2: Get the location of JDK executable (Java Compiler)
- Step 3: Setting JAVA_HOME variable
How to Set JAVA_HOME for JDK & JRE: A Step-by-Step Guide
This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. 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 323,880 times.
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 wikiHow article will show you the easiest ways to change or set the Java home path on Windows, macOS, and Linux.
- 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.
Windows
- 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.
- Press the Windows key on your keyboard and type advanced system .
- Click View advanced system settings in the search results.
Click the Environment Variables button. You’ll see it at the bottom-right corner of the System Properties window.
- 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.
- If you’re editing the current JAVA_HOME path, you’ll already have JAVA_HOME here. So, you can skip this step.
- 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.
- 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.
- 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.
- 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.
macOS
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.
- 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.
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.
- 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.
- 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.
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).
- If you had any other windows open that were attempting to find Java binaries, close and reopen them.
- Shopping for PC accessories or office supplies? Check out our coupon site for Staples discounts or our coupon site for Office Depot.
Linux
- 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.
- echo «export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-arm64» >> ~/.bashrc
- echo «export PATH=$PATH:$JAVA_HOME/bin» >> ~/.bashrc
Expert Q&A
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 .
You Might Also Like
Use Easy Windows CMD Commands to Check Your Java Version
How to Do Division in Java (Integer and Floating Point)
How to Compile and Run Java Programs Using Notepad++
Simple Steps to Type a Bunny with Your Keyboard
About This Article
This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. 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 323,880 times.
Is this article up to date?
Quizzes
You Might Also Like
Use Easy Windows CMD Commands to Check Your Java Version
How to Do Division in Java (Integer and Floating Point)
How to Compile and Run Java Programs Using Notepad++
How to Set JAVA_HOME Variable in Ubuntu Linux Correctly
If you are running Java programs on Ubuntu using Eclipse, Maven or Netbeans etc, you’ll need to set JAVA_HOME to your path. Otherwise, your system will complain that “java_home environment variable is not set”.
In this beginner’s tutorial, I’ll show the steps to correctly set Java Home variable on Ubuntu. The steps should be valid for most other Linux distributions as well.
The process consists of these steps:
- Making sure Java Development Kit (JDK) is installed.
- Finding the correct location of JDK executable.
- Setting the JAVA_HOME variable and making the change permanent.
Step 1: Check if JDK is installed
The simplest way to check if Java Development Kit (JDK) is installed on your Linux system is by running this command:
The above command checks the version of Java compiler. If it is installed, it will show the Java version.
If the command shows an error like javac command not found, you’ll have to install JDK.
If Java Compiler is not installed on your system, install Java Development Kit using this command:
sudo apt install default-jdk
This will install the default Java version in your current Ubuntu version. If you need some other specific Java version, you’ll have to specify it while installing Java on Ubuntu.
Once you have made sure that Java Compiler is present on your system, it’s time to find its location.
Step 2: Get the location of JDK executable (Java Compiler)
The executable is usually located in the /usr/lib/jvm directory. I won’t left you on your own for a guessing game. Instead, let’s find out the path of the Java executable.
Use the which command to get the location of Java Compiler executable:
The problem here is that the location it gives is actually a symbolic link. You’ll have to follow it a couple of times:
And when you find a path like /usr/lib/jvm/java-11-openjdk-amd64/bin/javac, you remove the /bin/javac from it to get something like /usr/lib/jvm/java-11-openjdk-amd64
An easier method is to follow the symbolic link and get to the actual executable file directly using this command:
readlink -f `which javac` | sed "s:/bin/javac::"
The readlink command follows a symbolic link. I have used ` around which java. This is called command substitution and it replaces the command with its output. Sed is then used to replace /bin/javac by nothing and thus removing it altogether.
In my example, the location of the executable file is /usr/lib/jvm/java-11-openjdk-amd64. It could be different for you. Copy the correct path you got from the above command in your system. You know, you can copy paste in the Ubuntu terminal.
Step 3: Setting JAVA_HOME variable
Now that you have got the location, use it to set the JAVA_HOME environment variable:
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/bin/java
Check the value of JAVA_HOME directory:
Try to run your program or project in the SAME TERMINAL and see if it works.
This is not over yet. The JAVA_HOME variable you just declared is temporary. If you close the terminal or start a new session, it will be empty again.
To set JAVA_HOME variable ‘permanently’, you should add it to the bashrc file in your home directory.
You can use the Nano editor for editing files in the Linux terminal. If you do not want that and take a simple copy-paste approach, use the following commands:
Back up your bashrc file (in case you mess it, you can get it back):
Next, use the echo command to append the export command you used at the beginning of this section. Change the command below to use the correct path as displayed by your system in.
echo "export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64" >> ~/.bashrc
Verify that it has been correctly added to the end of the file:
The above tail command will show the last 3 lines of the specified file.
Here’s the entire output of the above three commands.
Now, even if you exit the session or restart the system, the JAVA_HOME variable will still be set to the value you specified. That’s what you want, right?
Do note that if you change the default Java version in the future, you’ll have to change the value of JAVA_HOME and point it to the correct executable path.
I hope this tutorial not only helped you to set Java Home, it also taught you how you are doing it.
If you are still facing issues or have any questions or suggestions, please let me know in the comments.