How to set the Java PATH and JAVA_HOME variables in Windows

If you don’t have Java in the PATH variable, when you try to run a Java program (with java command) or compile (with javac command), you’ll get an error message like: ‘java’ is not recognized as an internal or external command.

The solution is to add the JDK bin location to the PATH system variable so you don’t have to type the full path to the Java executables (java.exe and javac.exe). Furthermore, some development tools depend on another system variable called JAVA_HOME, so you might as well add this too.

Here’s how to add these two system variables:

  • Open the Environment Variables configuration in Windows (search for “Edit the system environment variables” and click on it).
  • In the System variables section (at the bottom), click New
  • In the Edit System Variable window:
    • Variable name: JAVA_HOME
    • Variable value: C:\Program Files\Java\jdk-19
    • Click OK to save
  • Now edit the Path system variable (in System variables grid, double-click on the Path variable):
    • Click New
    • Put %JAVA_HOME%\bin
    • Click OK to save.
  • Now click OK to exit the Environment Variables window.

Note: Be sure to put the path where you installed the JDK. I installed mine here C:\Program Files\Java\jdk-19.

Now as a quick test, open a new command prompt and check the Java version:

java -version
Code language: Bash (bash)

It should output the version info, as expected:

openjdk version "19" 2022-09-20 OpenJDK Runtime Environment (build 19+36-2238) OpenJDK 64-Bit Server VM (build 19+36-2238, mixed mode, sharing)
Code language: plaintext (plaintext)

If you’re still getting the ‘java’ is not recognized error, make sure you open a NEW command prompt. You can’t use a command prompt that was already open before you edited the system variables (because they’re cached).

Leave a Comment