Note: If you already set the Java Path, but it’s showing an ERROR: JAVA_HOME is set to an invalid directory, then scroll to the bottom for answer.
To run Java from any directory, you need to set the Java path in environment variables, and here’s how you can do it.
How to set Java Path in Windows 11
Windows 11 is the latest upgrade to Windows 10, but setting the Java Path in Windows 11 is still the same.
First, download and install the JAVA from the oracle official website (recommended JDK over JRE)
After the successful installation, navigate to the folder where you installed the Java.
C:\Program Files\Java or C:\Program Files (x86)\Java
From the Java folder, navigate to the \bin directory and copy the path.
Click on Start and search for environment variables.
Select Edit the system environment variables from the search results.
Click on the Environment Variables at the bottom right corner.
Later a new Window will appear where you will see two sections, User variables, and System variables. Click on the New button under the System variables section.
Now, you’ll see two text fields, Variable name, and Variable value.
- The Variable name should be JAVA_HOME, and in the Variable value field, paste the Java bin directory path that you copied earlier.
Now, the Java path is set on your Windows 11, and restart your PC once.
Even after setting the environment variables if you are getting errors while running Java, here’s what to do.
ERROR: JAVA_HOME is set to an invalid directory
Two main reasons led to this error.
- Either you have entered an invalid directory or Java is not installed properly
If you entered your Java_home Directory as C:\Program Files\Java\bin and if it isn’t working, then try removing the bin and make sure to restart the terminal. You can change the JAVA_HOME directory by following the steps explained at the beginning.
Even after removing the bin, if you are getting the same error, then try reinstalling Java. It might be an installation failure that is throwing the error.
Perform a clean installation
- Uninstall the existing version of JAVA
- Restart the PC
- Once again newly Download the JAVA from the official website and perform the installation