US Trends

what is classpath in java

Quick Scoop: In Java, the classpath is the list of locations where the Java compiler and JVM look for classes and libraries when compiling or running a program. Those locations can be folders, JAR files, or ZIP files, and the default classpath is usually the current directory .

What it does

The classpath tells Java where to find your code and its dependencies. If a required class is not on the classpath, you can get errors like ClassNotFoundException or compilation failures.

Common ways to set it

  • Command line: using -cp or -classpath.
  • Environment variable: CLASSPATH.
  • IDE settings: your editor or build tool often manages it for you.

Simple example

If your project uses classes stored in lib/ and out/, you might run something like:

bash

java -cp out:lib/* com.example.Main

On Windows, the separator is usually ; instead of :.

Classpath vs module path

Classpath is the older, traditional mechanism for finding classes. In Java 9 and later, the module path is used for modular applications and adds stricter dependency rules and boundaries.

If you want, I can also give you a very beginner-friendly “classpath vs package vs module” explanation.