Master java skills

How to set ‘Path’ env variable

Before knowing how to set ‘Path’ environment variable for Java executables, we need to know why is it mandatory? The answer is – it is not mandatory but it makes it easier for us to run java programs and some of the other utilities which use JAVA_HOME environment variables to run.

Path variable is one of the environment variables in widows that has comma separated values. In order to execute any utility or executable program such as java, mvn from command prompt, we need to tell windows where that executable file exist. The location of that executable is required to be mentioned in ‘Path’ variable.

If we do not mention the path doesn not mean we cannot run a java program. We still can, but then we have to give the complete path of java.exe or javac.exe file like below

Here we have given complete path of javac.exe

But this is not a handy approach. We have to give complete path everytime we want to run a command. That’s why set environment variable once and we are good to run all java commands until the machine is formatted or the environment variable ‘Path’ is mistakenly modified.

Quiz: what happens when neither path varible is set nor complete path is given? Find answer in the below screenshot:

Steps to set ‘Path’ environment variable

  1. Click on the windows button and start typing ‘environment..’ Below window will appear. Click on ‘Edit the system evnironment variables’

2. In the below window, click on ‘Environment Variables…’ button.

3. Following window will open, click on ‘New’ button

4. First, we will set a new variable named as ‘JAVA_HOME’. Its value would be the location where jdk is installed. In this case, jdk is installed in a folder c:\Oracle\java

5. This would be added to the list as shown below

6. Now select ‘Path’ variable and click on ‘Edit’ button. Below window will open. The list variables would not be same as below snapshot but window would be same. Here, we have to click on new

7. Add a new entry like below. We have to mention %JAVA_HOME%\bin. Meaning, we have to provide the path to the bin folder inside our java installation directory.

8. Save these settings by clicking ‘OK’ button wherever applicable. This is how we have set ‘Path’ variable for Java. Same steps can be followed to include maven, oracle etc in the ‘Path’.

Note -> Using the above steps we have set the ‘Path’ variable permanently. We can also set it temporarily. The steps are given below.

How to set ‘Path’ environment variable temporarily

Temporary ‘Path’ can be set on one command prompt session. Once the command prompt is closed, the original setting of ‘Path’ will be in effect.

  1. Open command prompt and run below command: echo %path%

2. Copy the echoed path to a notepad or any other text editor and add path to your jdk\bin directory. And run the below command (as mentioned in the below screenshot)

set path = <existing-path>;C:\Oracle\java\bin;

In this way, ‘Path’ variable is set, but it is applicable only for this command prompt window.