refresh java logo

What is Path and Classpath Variable

Once you have installed the java, the next step for you would be to set Path variable for java. Before setting the Path variable Let's understand what it is :

Path is an environment variable in an operating system. This variable stores a list of directories separated by semicolons(;) as value. These directories points to locations where executable files are available. Once a command is executed in a terminal, operating system looks for the corresponding executable in these directories.

In java, executable files are available in bin(for eg. C:\Program Files\Java\jdk1.8.0_102\bin) folder of java installation directory. This folder contains executables like javac.exe, java.exe, javap.exe, javadoc.exe etc. These executables are used for different types of tasks in java. For example javac.exe is used for compilation of java program, java.exe used for execution of java program and so on.

If Path variable is not set, you need to move to the directory containing executables to execute the command in terminal. Once Path variable is set, java commands or JDK tools can be executed from any directory in a terminal. Let's see how to set Path variable in different operating system.

Windows 7

1. On desktop, right click the Computer icon, then choose Properties from the context menu.

computer icon

2. Click the Advanced system settings link.

advance system settings

3. Click Environment Variables in Advanced section.

environment variable window

4. In System Variables section, find the PATH environment variable and select it. Click Edit. Add PATH environment variable value(Java installation directory upto bin folder) after a semicolon(;). If PATH environment variable does not exist, click New. In New System Variable window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.

system variable window

Is Path variable name case sensitive ?

No, it's not case sensitive. You can use any form eg. PATH, Path, path etc.

What is the difference between System variables and User variables ?

If you set Path variable in User variables section, it will be applicable for that user only while if you set it in System variables section it will be applicable for all users on that system.

Windows XP

1. Select Start, select Control Panel. double click System, and select the Advanced tab.
2. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New .
3. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.

Linux

1. Go to your home directory
    cd $HOME
2. Open the .bashrc file.
3. Add the following line at the end of file. The installation directory(/usr/local/jdk1.8.0_102/bin) given in below command is just an example, You need to use the java installation directory of your system.
    export PATH=/usr/local/jdk1.8.0_102/bin:$PATH
4. Save the file and exit from the file. Execute below command to apply the changes immediately else you need to login again in your system.
    source .bashrc

The steps given above will set the Path variable only for current user. If you want to set Path for all users, you need to log in as root in the bash shell and perform the above steps on the .profile file in the etc directory and not the .bashrc file in the home directory.

What if I am using any other operating system ?

If you are using any other operating system, you can search on google and follow the links to set Path variable for that operating system.

CLASSPATH Variable

If you are a beginner you can refer this section after My First Java Program, as classpath variables are used while running the java programs. Let's understand what a classpath variable is:

In java after compilation of a program, a .class file is generated. The CLASSPATH variable is a way to tell the java commands(JDK tools) or applications, where to look for user classes(.class files) while running the program. There are two ways to set the CLASSPATH variable value, first you can set this as an environment variable in OS, second you can use -cp or -classpath command line option in command window itself.

The preferred way to specify the class path is by using the -cp command line option. This allows the CLASSPATH to be set individually for each program without affecting other programs. The default value of the class path is current directory ("."), which means only current directory is searched for .class files. Specifying either the CLASSPATH variable or the -cp command line option overrides the default value("."). Use the preferred way, which is -cp command line option if class files are not stored in the directory where you are running java commands. To add or edit the CLASSPATH variable as an environment variable, use the same procedure used for the PATH variable.

Is classpath variable name case sensitive ?

If you are setting classpath as environment variable of operating system then it's not case sensitive, you can use ClASSPATH, Classpath, classpath etc as variable name. But if you are using in command window directly while running java program then it's case sensitive, you will have to use classpath, not CLASSPATH, Classpath etc.