Setting Path Environment Variable

In the last topic, we have seen how to compile and run java programs saved in the bin folder of JDK installation directory. Now we will discuss how to compile and run java programs saved in different folders other than bin folder of JDK installation directory

We will also learn how to set path environment variable.

Let us consider following java program.

class Sample
{
     public static void main(String[] args)
     {
          System.out.println("I am saved in different folder");
     }
}

Type the above program in notepad and save as “Sample.java” in a folder other than bin folder of JDK installation directory. . I will save it in C:\Sources in my systemThe path of bin folder in my system is C:\Program Files\Java\jdk1.8.0_241\bin

Now open the Command Prompt and go to the folder where you have saved your java file, using CD command.

C:\>CD Sources                                                         ——->(In My System)

If you type javac command from this location, you will get an exception saying that javac command is not recognised as internal or external command. To run javac or java command from this location, you need to set path variable to bin folder of JDK installation directory.

Setting path using command prompt

To set the path via command prompt type the below command

C:\Sources> set path=C:\Program Files\Java\jdk1.8.0_241\bin

To check whether the path has been set or not use echo command

C:\Sources> echo %path%                   ——–> This will show the value of the path variable

By setting the path variable you are telling the java compiler that your source file is located in C:\Sources location.

Now Type javac and java command to compile and run the program.

C:\Sources> javac Sample.java

C:\Sources> java Sample

If everything works fine you will get the output as below:

Output
I am saved in different folder

Now one very important thing to notice is that when you open another instance of commandprompt and type command javac or java, you will again get  javac command is not recognised as internal or external command.

You again have to set the path variable for running your program.

Setting path variable globally

To avoid the setting the path variable each time you open the command prompt, what you need to do is set Path Environment Variable for whole system.

To set Path Environment Variable for the whole system, click on windows icon on windows 10 and type “environment” You will see the first option as “edit the system environment variables” Click on it.

System Properties —> Advanced —> Environment Variables —> choose to set for either user or for whole system —> If the path variable already exist click on Edit otherwise click on New —> If you have clicked on New, enter variable name as path and variable value as path to bin folder of JDK installation directory.  If you clicked on Edit, append “;” and path of bin folder of JDK installation directory to alraedy existed value. —> Click on OK

Now, you can use java and javac commands without setting path variable in your command prompt.

Leave a Reply