How to Set the Java Path?A Step-by-Step Guide

Why Do We Need to Set the Java Path?

  • The Java path permits us to run Java programs from any registry without having to indicate the total way of the javac (compiler) and java (interpreter) commands each time.
  • By setting the way, you make the bin folder of the Java Development Kit (JDK) available system-wide.

How to Set the Java Path?

Method 1: Temporary set the java Path (For Single Session)

Temporary path settings will last only until the Command Prompt is open. Once closed, the settings are lost.

Steps:

  1. Find Your JDK Path:
    • Locate the folder where your JDK is installed. Typically, it will be:
      makefile
      C:\Program Files\Java\jdk-XX.X.X\bin
      Replace XX.X.X along with your JDK version (e.g., jdk-20.0.1).
  2. Open Command Prompt:
    • Press Windows + R, type cmd, and hit Enter.
  3. Set the Path Temporarily:
    • In the Command Prompt, type:
      cmd

       

       

      set path=C:\Program Files\Java\jdk-XX.X.X\bin

      Example:

      cmd
      set path=C:\Program Files\Java\jdk-20.0.1\bin
  4. Verify the Path:
    • To confirm the path is set, type:
      cmd
      javac -version
    • If the version number appears, the temporary path is set.

Method 2: Permanent Path (For All Sessions)

Setting the path permanently ensures that the JDK tools are always available from the command line, even after restarting your system.

Steps:

  1. Locate Your JDK Installation Directory:
    • Navigate to the folder where your JDK is installed, typically:
      makefile
      C:\Program Files\Java\jdk-XX.X.X\bin
  2. Open Environment Variables:
    • Right-click This PC or My Computer (on the desktop or File Explorer).
    • Select Properties.
    • In the left menu, click Advanced system settings.
    • Under the Advanced tab, click Environment Variables.
  3. Edit the System Path Variable:
    • In the System Variables section, find Path and click Edit.
    • If Path doesn’t exist, click New and create it.
  4. Add the JDK Path to Path Variable:
    • In the Edit Environment Variable window, click New.
    • Paste the complete path to the JDK bin directory:
      makefile
      C:\Program Files\Java\jdk-XX.X.X\bin
    • Click OK to save changes.
  5. Verify the Path:
    • Open a new Command Prompt.
    • Type:
      cmd
      javac -version
    • The version number of the javac tool should display, confirming that the path is set successfully.

Additional Information

  1. Path Not Working?
    • Ensure there are no typos in the directory path.
    • The JDK must be installed correctly. If unsure, reinstall the latest version.
  2. Alternative Way Using Command Line:
    • Use the following to directly append the path using the Command Prompt (requires Admin privileges):
      cmd
      setx path “%path%;C:\Program Files\Java\jdk-XX.X.X\bin”
  3. Important Notes:
    • Always add ; between existing paths and the new one in the Path variable.
    • Never delete existing entries in the Path variable, as it might cause other programs to stop working.

Key Points to Remember:

  • Temporary Path is suitable for testing but needs to be re-applied each time.
  • Permanent Path saves time and effort in the long run.
  • Always verify with javac -version after setting the path

 

Leave a Comment