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:
- Find Your JDK Path:
- Locate the folder where your JDK is installed. Typically, it will be:
- Open Command Prompt:
- Press Windows + R, type
cmd
, and hit Enter.
- Press Windows + R, type
- Set the Path Temporarily:
- In the Command Prompt, type:
- Verify the Path:
- To confirm the path is set, type:
- 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:
- Locate Your JDK Installation Directory:
- Navigate to the folder where your JDK is installed, typically:
- 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.
- 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.
- Add the JDK Path to Path Variable:
- In the
Edit Environment Variable
window, click New. - Paste the complete path to the JDK bin directory:
- Click OK to save changes.
- In the
- Verify the Path:
- Open a new Command Prompt.
- Type:
- The version number of the
javac
tool should display, confirming that the path is set successfully.
Additional Information
- Path Not Working?
- Ensure there are no typos in the directory path.
- The JDK must be installed correctly. If unsure, reinstall the latest version.
- Alternative Way Using Command Line:
- Use the following to directly append the path using the Command Prompt (requires Admin privileges):
- 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.
- Always add
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