Path and Classpath in Java

Once you have installed the java, the next step for you would be to set Path variable for java. Before setting the Path variable, first 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(eg. CMD), 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.

How to set java path in Windows 10

  • On desktop, right click the This PC icon, then choose Properties from the context menu. If This PC icon is not present on desktop, then search it by writing "This PC" in search panel given at bottom left of windows and then right clik on This PC icon and choose Properties.

    computer icon
  • Now click the Advanced system settings link given in left side.

    advance system settings in windows 10
  • Click Environment Variables in Advanced section.

    environment variable window
  • Now in new pop up window, inside System Variables section, find the Path environment variable and select it, if it's not there then follow the next step. Click Edit. In new pop up window, click New. Add Path environment variable value(Java installation directory upto bin folder). Click OK. Close all remaining windows by clicking OK.

    edit system variable window
  • If Path environment variable does not exist in System Variables section, click New. In New System Variable pop-up, specify the variable name as Path and variable value as the location of java installation directory. Click OK. Close all remaining windows by clicking OK.

    add system variable window

How to set java path in Windows 7

The steps in windows 7 is very similar as windows 10. Follow below steps to set path variable in windows 7.

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

  • Click the Advanced system settings link.

  • Click Environment Variables in Advanced section.

  • 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 in windows 7

Is Path variable case sensitive ?

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

What is the difference between user variables and system 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.

How to set java path in Windows XP

  • Select Start, select Control Panel. double click System, and select the Advanced tab.
  • 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.
  • 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.

How to set java path in Linux

  • Go to your home directory
        cd $HOME
  • Open the .bashrc file.
  • 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
  • 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 user 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.

CLASSPATH Variable in Java

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

In java after compilation of a program, a .class file is generated. This .class file is used while running that program. The CLASSPATH variable is a way to tell the Java Virtual Machine(JVM) 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.

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.

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 classpath is current directory ("."), the directory in which the java command is executed inside the terminal(eg. cmd) to run the program. In this case JVM looks the current directory 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.

Difference between path and classpath in Java

Path is an environment variable which is used by the operating system to find the java executables. One needs to set the location containing java executables(eg. java.exe, javac.exe, javap.exe) etc in path variable in order to compile/run programs in java.

Classpath is a variable which is used to tell the java virtual machine(jvm) where to look for .class files while running the java program. When you compile a program in java, the java compiler generates a .class file and when you run the program, the java virtual machine loads this .class file inside memory to execute the program. The Classpath variable can be used to tell the jvm where to look for .class file at runtime.