Updating the PATH Environment Variable in Windows

You can always specify the full path to an executable file (like javac or java) every time you run it, as shown below in compiling and running the class HelloPrinter:

C:\> "C:\Program Files\Java\jdk1.8.0_11\bin\javac" HelloPrinter.java
C:\> "C:\Program Files\Java\jdk1.8.0_11\bin\java" HelloPrinter
Hello World!

To be more efficient, however, we can take advantage of a special environment variable called "PATH". Essentially, when you type a command into the command line, the computer will search the working directory for that command. If it doesn't find it there, it will search any directories identified by your PATH environment variable too. If it still doesn't find the command you typed, it finally gives up and reports an error.

What folllows shows what a typical PATH variable might look like -- although they can vary quite a lot from machine to machine, depending on what software has been installed:

PATH = "C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Java\jdk1.8.0_11\bin"

As can be seen, the structure of the PATH variable is pretty simple -- it is just a bunch of directories separated by semi-colons.

So, for example, if C:\Program Files\Java\jdk1.8.0_11\bin was a directory identified by the PATH variable (as it is above), we could compile and run the aforementioned HelloPrinter class with:

C:\> javac HelloPrinter.java
C:\> java HelloPrinter

If we have to use certain commands regularly, we will save a ton of time typing by including the directory paths to these commands in this special PATH variable. To change the PATH variable on MS Windows permanently (so it will even persist after rebooting) simply do the following:

  1. Open the Start Menu, and click "Control Panel", then the "System" icon.

  2. Click "Advanced system settings", then "Environment Variables..."

  3. Find the line under "System variables" that corresponds to the "Path" variable, select it, and click "Edit..." Advance to the very end of the text listed in the "Variable value" text field, and then add the location of the bin folder of the JDK installation to the end. Remember, the directories listed in the path variable should be separated by semicolons ";", so you may need to add one. Click "OK" when you are done (probably 3 times to clear all the windows). The following shows a typical value for the PATH variable, with the bin folder added on the end in green:

    C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Java\jdk1.8.0_11\bin

Additional notes

  • The PATH environment variable is not case-sensitive.
  • Windows looks for programs in the PATH directories in order, from left to right.
  • You should only have one bin directory for a JDK in the path at a time. Those following the first instance are ignored.
  • If you are not sure where to add the path, add it to the end of the PATH variable.
  • The new path takes effect in each new command or powershell window you open after setting the PATH variable.