HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Basics / Java Classpath

Java Classpath

Learn how to set classpath in Java either as an environment variable and pass as the command-line argument. During runtime of any Java application, the CLASSPATH is a parameter that tells the JVM where to look for classes and packages.

  • The default value of the classpath is “.” (dot), meaning that only the current directory is searched for dependencies.
  • Specifying either the CLASSPATH environment variable or the -cp command line switch overrides this value.
  • The order in which you specify multiple classpath entries is important. The Java interpreter will look for classes in the directories in the order they appear in the classpath variable.

Java Classpath separators are OS specific.

Windows – ; [Semicolon]
Linux/Unix – : [Colon]

1. Setting CLASSPATH as Environment Variable

When you have set the location of jar files that are always required during the application runtime, then it’s probably the best to add them in the machine’s environment variable 'CLASSPATH'.

During application runtime, application class loader will always scan the jar files and classes at specified paths in this variable.

To set CLASSPATH environment variable, find the location of user environment variables in your machine and add all paths where Jar files are stored. Use the separator between different two folders, jar files or classes.

You can find the user environment variables window by –

  1. From the desktop, right click the Computer icon.
  2. Choose Properties from the context menu.
  3. Click the Advanced system settings link.
  4. Click Environment Variables. In the section System Variables, find the CLASSPATH environment variable and select it. Click Edit. If the CLASSPATH environment variable does not exist, click New.
  5. Add all folders separated with separator. Click OK. Close all remaining windows by clicking OK.
System Properties
System Properties

If you are creating CLASSPATH for the first time, you need to specify the name for Variable Name in the Windows 10. Use '.' (dot) to denote current directory.

2. Setting CLASSPATH from Command Line

Use -classpath argument to set classpath from command prompt/console. Use below given commands to set classpath for different requirements.

Let’s say we have a folder named dependency where JAR files and other classes are placed.

2.1. Add a single jar file in classpath

Below syntax examples will add single jar file in classpath.

//WINDOWS
$ set CLASSPATH=.;C:\dependency\framework.jar

//Linux/Unix
$ export CLASSPATH=.:/dependency/framework.jar

2.2. Add multiple jar files in classpath

Below syntax examples will add more than one jar file in classpath. To do so, simply use the delimiter for your operating system (either ; or :) as a separator between the locations specified for the CLASSPATH.

To add all JAR files present in a directory, use wildcard character ('*').

//WINDOWS
$ set CLASSPATH=C:\dependency\framework.jar;C:\location\otherFramework.jar 				
$ set CLASSPATH=C:\dependency\framework.jar;C:\location\*.jar

//Linux/Unix
$ export CLASSPATH=/dependency/framework.jar:/location/otherFramework.jar  	
$ export CLASSPATH=/dependency/framework.jar:/location/*.jar

2.3. Add multiple classes to classpath

Many times, you may need to add individual classes in classpath as well. To do so, simply add the folder where classfile is present. e.g. let’s say there are five .class files are present in location folder which you want to include in classpath.

//WINDOWS
$ set CLASSPATH=C:\dependency\*;C:\location

//Linux/Unix
$ export CLASSPATH=/dependency/*:/location

As a best practice, always organize all JAR files and application classes inside one root folder. This may be the workspace for the application.

Please note that subdirectories contained within the CLASSPATH would not be loaded. In order to load files that are contained within subdirectories, those directories and/or files must be explicitly listed in the CLASSPATH.

2.4. Clearing Classpath

If your CLASSPATH environment variable was set to a value that is not correct, then you can unset CLASSPATH with specifying empty value to it.

set CLASSPATH=

3. Executing programs with ‘-classpath’ or ‘-cp’ option in Java

Apart from setting classpath to the environment variable, you can pass additional classpath to Java runtime while launching the application using –classpath option or –cp option.

Use the . (dot) to include the current path into the classpath where the .class file has been generated.

$ javac –classpath C:\dependency\framework.jar MyApp.Java
$ java –classpath .;C:\dependency\framework.jar MyApp

4. How to Find and Print CLASSPATH Value

Anytime you wish to verify all path entries in CLASSPATH variable, you can verify using echo command.

//Windows
c:/> echo %CLASSPATH%

//Linux/Unix
$ echo $CLASSPATH

If CLASSPATH is not set you will get a CLASSPATH: Undefined variable error (Solaris or Linux) console or simply %CLASSPATH% printed in windows command prompt.

Happy Learning !!

Read More:

Java – How to set classpath in Windows 7, 8, 10
Java – Set classpath from command line

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. PK

    July 26, 2020

    There is a subtle but serious bug in the code outlined in section 3 that can be instructive to understanding that classpath is where JVM looks for all classes and not just for dependant classes.
    The first statement “$ javac –classpath C:\dependency\framework.jar MyApp.Java” compiles fine and places the MyApp.class file in the current directory. However, the second statement “$ java –classpath C:\dependency\framework.jar MyApp” fails with error unable to find MyApp class and requires updating the classpath including the location of MyApp.class which is the current directory in this case.

    • Lokesh Gupta

      July 27, 2020

      Thanks for the feedback. Updated the article. It requires to run the javac and java command from the same directory where the java file is placed.

  2. Velliyangiri

    August 10, 2019

    How to set the classpath through our java program dynamically?

  3. libesse

    April 10, 2019

    //Linux/Unix
    $ export CLASSPATH=/dependency/framework.jar:/location/otherFramework.jar  
    $ export C:\dependency\framework.jar:C:\location\*.jar
    

    should be

    //Linux/Unix
    $ export CLASSPATH=/dependency/framework.jar:/location/otherFramework.jar  
    $ export CLASSPATH=/dependency\framework.jar:/location/*.jar
    
  4. Anonymous

    July 16, 2018

    Setting Classpath on linux/unix is not working

  5. David

    April 11, 2018

    javac –classpath C:\dependency\framework.jar:C:\location\otherFramework.jar

    This costed me a lot of time, because its wrong. On windows, the classpath separator is “;”.

  6. Anurag Singh

    July 21, 2017

    this is really very nice and beautiful article.
    thanks for share this.

Comments are closed on this article!

Search Tutorials

Java Basics

  • Java Introduction
  • Java Installation
  • Java Hello World
  • Java JDK, JRE and JVM
  • Java Classes and Objects
  • Java ClassPath
  • Java Operators
  • Java Data Types
  • Java Primitive Types
  • Java Variables
  • Java Comments
  • Java main() Method
  • Java Command Line Args
  • Java Wrapper Classes
  • Java Types of Statements
  • Java Block Statement
  • Java Pass-by-Value
  • Java System Properties
  • Java Static Import
  • Java hashCode() and equals()
  • Java Immutable Class
  • Java 32-bit vs 64-bit
  • Java java.exe vs javaw.exe
  • Java Generate Bytecode
  • Java Naming Conventions
  • Java Little-Endian vs Big-Endian

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces