Categories: Java

Setting CLASSPATH in Java

In Java, you can set the CLASSPATH variable.

CLASSPATH:

Application ClassLoader uses CLASSPATH as an environment variable to locate and load .class files. The CLASSPATH specifies the location of third-party and user-defined classes that are not extensions or components of the Java platform. When setting the CLASSPATH, make sure to include all directories that contain .class files and JAR files.

If the following conditions apply, you must set the CLASSPATH:

  • You must load a class that isn’t in the current directory or any of its subdirectories.
  • You need to load a class that isn’t in the extensions mechanism’s specified location.

A dot is the default value for CLASSPATH (.). It refers to the only directory that is currently being searched. When you set the CLASSPATH variable or use the -classpath command, the default value of CLASSPATH takes precedence (for short -cp). If you want to include the current directory in the search path, add a dot (.) to the new setting.

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.

Step 2: Click on Advanced System Settings.

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

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 Windows Using Command Prompt

Type the following command in your Command Prompt and press enter

set CLASSPATH=%CLASSPATH%;C:\Program Files\Java\jre1.8\rt.jar;

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

Note: also read about the What is Java programming language

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago