How to Set CLASSPATH in Java

CLASSPATH: CLASSPATH is an environment variable which is used by Application ClassLoader to locate and load the .class files. The CLASSPATH defines the path, to find third-party and user-defined classes that are not extensions or part of Java platform. Include all the directories which contain .class files and JAR files when setting the CLASSPATH.

You need to set the CLASSPATH if:

  • You need to load a class that is not present in the current directory or any sub-directories.
  • You need to load a class that is not in a location specified by the extensions mechanism.

The CLASSPATH depends on what you are setting the CLASSPATH. The CLASSPATH has a directory name or file name at the end. The following points describe what should be the end of the CLASSPATH.

  • If a JAR or zip, the file contains class files, the CLASSPATH end with the name of the zip or JAR file.
  • If class files placed in an unnamed package, the CLASSPATH ends with the directory that contains the class files.
  • If class files placed in a named package, the CLASSPATH ends with the directory that contains the root package in the full package name, that is the first package in the full package name.

The default value of CLASSPATH is a dot (.). It means the only current directory searched. The default value of CLASSPATH overrides when you set the CLASSPATH variable or using the -classpath command (for short -cp). Put a dot (.) in the new setting if you want to include the current directory in the search path.

If CLASSPATH finds a class file which is present in the current directory, then it will load the class and use it, irrespective of the same name class presents in another directory which is also included in the CLASSPATH.

If you want to set multiple classpaths, then you need to separate each CLASSPATH by a semicolon (;).

The third-party applications (MySQL and Oracle) that use the JVM can modify the CLASSPATH environment variable to include the libraries they use. The classes can be stored in directories or archives files. The classes of the Java platform are stored in rt.jar.

There are two ways to ways to set CLASSPATH: through Command Prompt or by setting Environment Variable.

Let's see how to set CLASSPATH of MySQL database:

Step 1: Click on the Windows button and choose Control Panel. Select System.

How to Set CLASSPATH in Java

Step 2: Click on Advanced System Settings.

How to Set CLASSPATH in Java

Step 3: A dialog box will open. Click on Environment Variables.

How to Set CLASSPATH in Java

Step 4: If the CLASSPATH already exists in System Variables, click on the Edit button then put a semicolon (;) at the end. Paste the Path of MySQL-Connector Java.jar file.

If the CLASSPATH doesn't exist in System Variables, then click on the New button and type Variable name as CLASSPATH and Variable value as C:\Program Files\Java\jre1.8\MySQL-Connector Java.jar;.;

Remember: Put ;.; at the end of the CLASSPATH.

How to Set CLASSPATH in Java

Difference between PATH and CLASSPATH

PATHCLASSPATH
PATH is an environment variable.CLASSPATH is also an environment variable.
It is used by the operating system to find the executable files (.exe).It is used by Application ClassLoader to locate the .class file.
You are required to include the directory which contains .exe files.You are required to include all the directories which contain .class and JAR files.
PATH environment variable once set, cannot be overridden.The CLASSPATH environment variable can be overridden by using the command line option -cp or -CLASSPATH to both javac and java command.

How to Set CLASSPATH in Windows Using Command Prompt

Type the following command in your Command Prompt and press enter.

In the above command, The set is an internal DOS command that allows the user to change the variable value. CLASSPATH is a variable name. The variable enclosed in percentage sign (%) is an existing environment variable. The semicolon is a separator, and after the (;) there is the PATH of rt.jar file.

How ext folder works in Java

The ext directory works a bit like the CLASSPATH. ext directory is the part of the class loading mechanism. The classes which are available within JARs in the ext directory are available to Java applications.

The following table demonstrates the key difference between the CLASSPATH and Extension Mechanism:

CharacteristicsCLASSPATHExtension Mechanism
Class loading orderCLASSPATH loads after bootstrap and extension loading.ext loads after bootstrap loading but before CLASSPATH loading.
ScopeIt is an application specific. All JREs on the host is the CLASSPATH environment variable.All JVMs are running in specific JRE java.ext.dirs.
Package namejava.class.path is used to find the directories and JAR archives containing class files.java.ext.dirs is used to specify where the extension mechanism loads classes.
SpecificationIt is specified by name including the extension.jar and directory containing .class files.All JAR files in specified directories are loaded.

The mechanism will pick up all .jar files from the extension directory even if the file does not have the .jar extension. The implementation of this is that if one can change the name of a jar placed in a classpath directory to have an extension other than .jar. The wildcard (*) does not pick it up. This technique will not work with the extension directory.

Let's understand the execution process through an example.

A.java

B.java

Compile the A.java file. we will archive the compiled A.class file into A.jar. Place this JAR file into another directory than the compiled B.class file.

To demonstrate the use of the classpath, we place the A.jar file in a directory C:\JavaPrograms and will access that JAR through wildcard (*) for B to use.

We found that B can still load the A.class while we had deleted it from the current directory. The Java launcher was explicitly looked for C:\JavaProgram. It is also possible to have the class loaded without its presence in the same directory and explicit classpath specification.

It is often referred to as a benefit of Using the extension mechanism because all applications which are using that JRE can see the same classes without the need to specify them on the classpath explicitly.

What happens if we change the name of A.jar into A.backup in the same CLASSPATH-referenced directory. NoClassDefFoundError is encountered when we do the same because the CLASSPATH-reference does not have the .jar extension.