In this article, I will describe the use of PATH Variable in Java.

Basically, the PATH variable in Java is an environment variable that specifies a list of directories where the operating system should look for executable files. So, in the context of Java, the PATH variable is used to specify the location of the java executable, which is used to run Java applications. Therefore, by adding the directory that contains the java executable to the PATH variable, you can run Java applications from any directory without having to specify the full path to the java executable every time you run a Java program.

An Example of Using PATH Variable in Java

In order to run a Java program, we can use PATH variable in a command-line interface (CLI). So, assuming that the java executable is located in C:\Program Files\Java\jdk1.8.0_202\bin, you would add this directory to your PATH environment variable. The following steps explains this process.

  1. At first, open the Command Prompt or terminal window on your computer.
  2. After that, type the following command to view the current value of your PATH variable:
echo %PATH%

3. Now, add the path to the java executable to your PATH variable by executing the following command:

set PATH=%PATH%;C:\Program Files\Java\jdk1.8.0_202\bin

4. Once, you add this path, verify that the PATH variable has been updated by executing the following command:

echo %PATH%

When the above command shows the correct path, you should be able to run the Java program from any directory by typing java <program-name> in the command prompt or terminal window.


Further Reading

Java Practice Exercise

programmingempire

Princites