Classpath

The classpath is the list of directories and JAR files the JVM searches when it needs to load a class. If a class isn't on the classpath, the JVM throws ClassNotFoundException or NoClassDefFoundError.

Setting the classpath

# Command-line flag (preferred):
java -cp lib/postgresql.jar:classes MyApp

# Windows uses ; instead of ::
java -cp "lib/postgresql.jar;classes" MyApp

# Environment variable (discouraged):
export CLASSPATH=lib/postgresql.jar:classes
java MyApp

JAR classpath

When running an executable JAR, the classpath is typically defined in the JAR's META-INF/MANIFEST.MF:

Main-Class: com.example.App
Class-Path: lib/postgresql.jar lib/jackson-core.jar
java -jar app.jar

Modules replace classpath (Java 9+)

Java 9 introduced the Java Platform Module System (JPMS). Modules have an explicit module-info.java declaring what they export and require. You load modules with --module-path (or -p) instead of -cp.

java --module-path mods -m com.example/com.example.App

Most applications still use the plain classpath; JPMS is adopted widely in the JDK itself and in some libraries but is not mandatory for application code.