ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.geeksforgeeks.org/java/different-ways-to-set-a-classpath-in-java/ |
| Last Crawled | 2026-04-14 21:50:32 (4 hours ago) |
| First Indexed | 2025-06-12 18:20:13 (10 months ago) |
| HTTP Status Code | 200 |
| Meta Title | Different Ways to Set a Classpath in Java - GeeksforGeeks |
| Meta Description | Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more., Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions. |
| Meta Canonical | null |
| Boilerpipe Text | Last Updated :
23 Jul, 2025
Java Virtual Machine(JVM)
uses classpath to locate the class files to execute the code. If your classpath is not set and the class file is not present in the same directory as your java file, then JVM will be unable to find the required class file, and it will throw an error (
java.lang.ClassNotFoundException
).
error caused while JVM is interpreting the code
Ways to Set a Classpath
There are five different ways to set a classpath. These are:
-cp
-classpath
--classpath
Temporary settings by using the 'set classpath' command
Permanent settings using environment variable window
The limitation of -
cp
,
-
classpath, and
--class-path
methods is that it can set classpath only for the current command line, in the next command line, if we access the desired class directly, we will get an Exception
Syntax:
>java -cp <directory_location> <class name>
Example:
// This code is located in F:\workspace\src
// It's class file is located in F:\workspace\bin
class
GFG
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"GFG!"
);
}
}
how to set classpath by using -cp command
Command Line Settings -
>java -cp <directory_location> <class name>
OR
>java -classpath <directory_location> <class name>
OR
>java --class-path <directory_location> <class name>
If we want to access classpath for all command lines, we must set the classpath command option.
The limitation of the
"set classpath"
command option is the classpath settings are available only for the current command prompt.
Temporary Settings:
>set classpath=<directory_location>
the temporary setting of classpath using 'set classpath' command
If we want to have classpath settings permanently for all the command prompts, we must set classpath in the environment variable section.
Permanent Settings:
Firstly, Right Click on "This PC".
Click Properties.
Click "Advanced System Settings".
Click "Environment Variables".
In the "User Variable Section", click the "New" button.
Enter Variable name :classpath [Don't give space between class path] Variable value:<directory_location>(for example in my F:\workspace\bin)
Click OK->OK->OK.
Close all windows, open a new command prompt, and run the java command
Environment Variables Setting
permanent classpath settings
We must include
.;
in the classpath beginning so that the
JVM
can access both the current working directory and the directory of the desired class file, respectively.
>java -cp <.;directory_location> <class name>
Importance of '.' in the classpath
If we set classpath pointing to one directory, if we don't place (dot), the classes available in the current working directory will not be found by compiler and
JVM
.
'.'
represents the current working directory. The current working directory means not the folder where you save the .java file, the folder path where you open the command prompt.
Syntax to include multiple paths in the classpath
We must separate each folder location with a
semicolon
separator.
java -cp ./folder1/*;./folder2/*;./folder3/* com.xyz.MainClass
Note -
The algorithm followed by the compiler and JVM for finding classes from classpath is
First Come First Execute.
Difference between path and classpath searching algorithm priority
Command prompt software will give first priority to present the working directory. In the present working directory, if the binary file is unavailable, only consider the path variable.
Compiler and
JVM
first look for classpath.
If a classpath is not created, then only they will search in the current working directory.
If a classpath is created, they don't look into the current working directory, only the classpath folder.
Note -
Generally in the Developer community, the first 4 types are recommended, they try to avoid using the permanent settings using the environment variable window. |
| Markdown | [](https://www.geeksforgeeks.org/)

- Sign In
- [Courses]()
- [Tutorials]()
- [Interview Prep]()
- [Java Tutorial](https://www.geeksforgeeks.org/java/java/)
- [Advanced Java](https://www.geeksforgeeks.org/advance-java/advanced-java/)
- [Interview Questions](https://www.geeksforgeeks.org/java/java-interview-questions/)
- [Exercises](https://www.geeksforgeeks.org/java/java-exercises/)
- [Examples](https://www.geeksforgeeks.org/java/java-programming-examples/)
- [Quizzes](https://www.geeksforgeeks.org/java/java-quiz/)
- [Projects](https://www.geeksforgeeks.org/blogs/java-projects/)
- [Cheatsheet](https://www.geeksforgeeks.org/java/java-cheat-sheet/)
- [DSA in Java](https://www.geeksforgeeks.org/dsa/dsa-in-java/)
- [Java Collection](https://www.geeksforgeeks.org/java/java-collection-tutorial/)
# Different Ways to Set a Classpath in Java
Last Updated : 23 Jul, 2025
**Java Virtual Machine(JVM)** uses classpath to locate the class files to execute the code. If your classpath is not set and the class file is not present in the same directory as your java file, then JVM will be unable to find the required class file, and it will throw an error ([java.lang.ClassNotFoundException](https://www.geeksforgeeks.org/java/how-to-solve-java-lang-classnotfoundexception-in-java/)).

error caused while JVM is interpreting the code
### Ways to Set a Classpath
There are five different ways to set a classpath. These are:
- \-cp
- \-classpath
- \--classpath
- Temporary settings by using the 'set classpath' command
- Permanent settings using environment variable window
The limitation of -**cp**,**\-**classpath, and **\--class-path** methods is that it can set classpath only for the current command line, in the next command line, if we access the desired class directly, we will get an Exception
**Syntax:**
```
>java -cp <directory_location> <class name>
```
**Example:**
Java
``

how to set classpath by using -cp command
### Command Line Settings -
```
>java -cp <directory_location> <class name>
```
**OR**
```
>java -classpath <directory_location> <class name>
```
**OR**
```
>java --class-path <directory_location> <class name>
```
- If we want to access classpath for all command lines, we must set the classpath command option.
- The limitation of the **"set classpath"** command option is the classpath settings are available only for the current command prompt.
### Temporary Settings:
```
>set classpath=<directory_location>
```

the temporary setting of classpath using 'set classpath' command
If we want to have classpath settings permanently for all the command prompts, we must set classpath in the environment variable section.
### Permanent Settings:
1. Firstly, Right Click on "This PC".
2. Click Properties.
3. Click "Advanced System Settings".
4. Click "Environment Variables".
5. In the "User Variable Section", click the "New" button.
6. Enter Variable name :classpath \[Don't give space between class path\] Variable value:\<directory\_location\>(for example in my F:\\workspace\\bin)
7. Click OK-\>OK-\>OK.
8. Close all windows, open a new command prompt, and run the java command

Environment Variables Setting

permanent classpath settings
##
We must include **.;** in the classpath beginning so that the **JVM** can access both the current working directory and the directory of the desired class file, respectively.
```
>java -cp <.;directory_location> <class name>
```
### Importance of '.' in the classpath
If we set classpath pointing to one directory, if we don't place (dot), the classes available in the current working directory will not be found by compiler and **JVM**. **'.'** represents the current working directory. The current working directory means not the folder where you save the .java file, the folder path where you open the command prompt.
### Syntax to include multiple paths in the classpath
We must separate each folder location with a **semicolon** separator.
```
java -cp ./folder1/*;./folder2/*;./folder3/* com.xyz.MainClass
```
> **Note -** The algorithm followed by the compiler and JVM for finding classes from classpath is **First Come First Execute.**
### Difference between path and classpath searching algorithm priority
- Command prompt software will give first priority to present the working directory. In the present working directory, if the binary file is unavailable, only consider the path variable.
- Compiler and **JVM** first look for classpath.
- If a classpath is not created, then only they will search in the current working directory.
- If a classpath is created, they don't look into the current working directory, only the classpath folder.
> **Note -** Generally in the Developer community, the first 4 types are recommended, they try to avoid using the permanent settings using the environment variable window.
Comment
[D](https://www.geeksforgeeks.org/user/dasdepesh/)
[dasdepesh](https://www.geeksforgeeks.org/user/dasdepesh/)
4
Article Tags:
Article Tags:
[Java](https://www.geeksforgeeks.org/category/programming-language/java/)
[java-basics](https://www.geeksforgeeks.org/tag/java-basics/)
### Explore
[](https://www.geeksforgeeks.org/)

Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)

Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
[](https://geeksforgeeksapp.page.link/gfg-app)[](https://geeksforgeeksapp.page.link/gfg-app)
- Company
- [About Us](https://www.geeksforgeeks.org/about/)
- [Legal](https://www.geeksforgeeks.org/legal/)
- [Privacy Policy](https://www.geeksforgeeks.org/legal/privacy-policy/)
- [Contact Us](https://www.geeksforgeeks.org/about/contact-us/)
- [Advertise with us](https://www.geeksforgeeks.org/advertise-with-us/)
- [GFG Corporate Solution](https://www.geeksforgeeks.org/gfg-corporate-solution/)
- [Campus Training Program](https://www.geeksforgeeks.org/campus-training-program/)
- Explore
- [POTD](https://www.geeksforgeeks.org/problem-of-the-day)
- [Job-A-Thon](https://practice.geeksforgeeks.org/events/rec/job-a-thon/)
- [Blogs](https://www.geeksforgeeks.org/category/blogs/?type=recent)
- [Nation Skill Up](https://www.geeksforgeeks.org/nation-skill-up/)
- Tutorials
- [Programming Languages](https://www.geeksforgeeks.org/computer-science-fundamentals/programming-language-tutorials/)
- [DSA](https://www.geeksforgeeks.org/dsa/dsa-tutorial-learn-data-structures-and-algorithms/)
- [Web Technology](https://www.geeksforgeeks.org/web-tech/web-technology/)
- [AI, ML & Data Science](https://www.geeksforgeeks.org/machine-learning/ai-ml-and-data-science-tutorial-learn-ai-ml-and-data-science/)
- [DevOps](https://www.geeksforgeeks.org/devops/devops-tutorial/)
- [CS Core Subjects](https://www.geeksforgeeks.org/gate/gate-exam-tutorial/)
- [Interview Preparation](https://www.geeksforgeeks.org/aptitude/interview-corner/)
- [Software and Tools](https://www.geeksforgeeks.org/websites-apps/software-and-tools-a-to-z-list/)
- Courses
- [ML and Data Science](https://www.geeksforgeeks.org/courses/category/machine-learning-data-science)
- [DSA and Placements](https://www.geeksforgeeks.org/courses/category/dsa-placements)
- [Web Development](https://www.geeksforgeeks.org/courses/category/development-testing)
- [Programming Languages](https://www.geeksforgeeks.org/courses/category/programming-languages)
- [DevOps & Cloud](https://www.geeksforgeeks.org/courses/category/cloud-devops)
- [GATE](https://www.geeksforgeeks.org/courses/category/gate)
- [Trending Technologies](https://www.geeksforgeeks.org/courses/category/trending-technologies/)
- Videos
- [DSA](https://www.geeksforgeeks.org/videos/category/sde-sheet/)
- [Python](https://www.geeksforgeeks.org/videos/category/python/)
- [Java](https://www.geeksforgeeks.org/videos/category/java-w6y5f4/)
- [C++](https://www.geeksforgeeks.org/videos/category/c/)
- [Web Development](https://www.geeksforgeeks.org/videos/category/web-development/)
- [Data Science](https://www.geeksforgeeks.org/videos/category/data-science/)
- [CS Subjects](https://www.geeksforgeeks.org/videos/category/cs-subjects/)
- Preparation Corner
- [Interview Corner](https://www.geeksforgeeks.org/interview-prep/interview-corner/)
- [Aptitude](https://www.geeksforgeeks.org/aptitude/aptitude-questions-and-answers/)
- [Puzzles](https://www.geeksforgeeks.org/aptitude/puzzles/)
- [GfG 160](https://www.geeksforgeeks.org/courses/gfg-160-series)
- [System Design](https://www.geeksforgeeks.org/system-design/system-design-tutorial/)
[@GeeksforGeeks, Sanchhaya Education Private Limited](https://www.geeksforgeeks.org/), [All rights reserved](https://www.geeksforgeeks.org/copyright-information/)
![]() |
| Readable Markdown | Last Updated : 23 Jul, 2025
**Java Virtual Machine(JVM)** uses classpath to locate the class files to execute the code. If your classpath is not set and the class file is not present in the same directory as your java file, then JVM will be unable to find the required class file, and it will throw an error ([java.lang.ClassNotFoundException](https://www.geeksforgeeks.org/java/how-to-solve-java-lang-classnotfoundexception-in-java/)).

error caused while JVM is interpreting the code
### Ways to Set a Classpath
There are five different ways to set a classpath. These are:
- \-cp
- \-classpath
- \--classpath
- Temporary settings by using the 'set classpath' command
- Permanent settings using environment variable window
The limitation of -**cp**,**\-**classpath, and **\--class-path** methods is that it can set classpath only for the current command line, in the next command line, if we access the desired class directly, we will get an Exception
**Syntax:**
```
>java -cp <directory_location> <class name>
```
**Example:**
``

how to set classpath by using -cp command
### Command Line Settings -
```
>java -cp <directory_location> <class name>
```
**OR**
```
>java -classpath <directory_location> <class name>
```
**OR**
```
>java --class-path <directory_location> <class name>
```
- If we want to access classpath for all command lines, we must set the classpath command option.
- The limitation of the **"set classpath"** command option is the classpath settings are available only for the current command prompt.
### Temporary Settings:
```
>set classpath=<directory_location>
```

the temporary setting of classpath using 'set classpath' command
If we want to have classpath settings permanently for all the command prompts, we must set classpath in the environment variable section.
### Permanent Settings:
1. Firstly, Right Click on "This PC".
2. Click Properties.
3. Click "Advanced System Settings".
4. Click "Environment Variables".
5. In the "User Variable Section", click the "New" button.
6. Enter Variable name :classpath \[Don't give space between class path\] Variable value:\<directory\_location\>(for example in my F:\\workspace\\bin)
7. Click OK-\>OK-\>OK.
8. Close all windows, open a new command prompt, and run the java command

Environment Variables Setting

permanent classpath settings
We must include **.;** in the classpath beginning so that the **JVM** can access both the current working directory and the directory of the desired class file, respectively.
```
>java -cp <.;directory_location> <class name>
```
### Importance of '.' in the classpath
If we set classpath pointing to one directory, if we don't place (dot), the classes available in the current working directory will not be found by compiler and **JVM**. **'.'** represents the current working directory. The current working directory means not the folder where you save the .java file, the folder path where you open the command prompt.
### Syntax to include multiple paths in the classpath
We must separate each folder location with a **semicolon** separator.
```
java -cp ./folder1/*;./folder2/*;./folder3/* com.xyz.MainClass
```
> **Note -** The algorithm followed by the compiler and JVM for finding classes from classpath is **First Come First Execute.**
### Difference between path and classpath searching algorithm priority
- Command prompt software will give first priority to present the working directory. In the present working directory, if the binary file is unavailable, only consider the path variable.
- Compiler and **JVM** first look for classpath.
- If a classpath is not created, then only they will search in the current working directory.
- If a classpath is created, they don't look into the current working directory, only the classpath folder.
> **Note -** Generally in the Developer community, the first 4 types are recommended, they try to avoid using the permanent settings using the environment variable window. |
| Shard | 103 (laksa) |
| Root Hash | 12046344915360636903 |
| Unparsed URL | org,geeksforgeeks!www,/java/different-ways-to-set-a-classpath-in-java/ s443 |