Quick revision
The short answer
The application class loader searches the class path entries in order, directories and jar files alike, and uses the first one that contains the requested class. -cp lib/* adds every jar directly in lib, but not subdirectories, and in no guaranteed order; with java -jar, -cp is ignored and the jar's manifest supplies Main-Class and the Class-Path list. When two jars contain the same class, the first one found wins, which hides version conflicts until a missing method fails at run time; the module path, by contrast, refuses split packages between named modules.
What to remember
In this answer, we saw that the application class loader searches the class path from left to right, directories and jars alike, and loads the first copy of each class it finds, after the JDK loaders have had their turn. A lib/* wildcard adds every jar directly in that directory in an unspecified order, java -jar ignores -cp and uses the manifest's Main-Class and Class-Path, and duplicates are silent because the first one wins. The module path behaves differently and refuses split packages between named modules at start-up.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The application class loader searches the class path entries in order, directories and jar files alike, and uses the first one that contains the requested class. -cp lib/* adds every jar directly in lib, but not subdirectories, and in no guaranteed order; with java -jar, -cp is ignored and the jar's manifest supplies Main-Class and the Class-Path list. When two jars contain the same class, the first one found wins, which hides version conflicts until a missing method fails at run time; the module path, by contrast, refuses split packages between named modules.
Why this question is asked
The class path is the first thing every Java developer configures and the last thing most of them examine. Yet a surprising number of production failures, a method that does not exist, a class from the wrong version, a main class that cannot be found, come down to how the JVM reads it. Interviewers ask this to check that we know the actual rules rather than the IDE's version of them. In this answer, we will recap what the class path is, walk through how the search works, cover wildcards, java -jar and the manifest, reproduce the duplicate class problem, and compare it with the module path.
1A Quick Recap of the Class Path
The class path is the list of places where the application class loader looks for our classes and resources. Each entry is either a directory, whose subfolders follow package names, so com.acme.App lives at com/acme/App.class under it, or a jar file, a zip archive with the same layout inside. Entries are separated by a colon on Linux and macOS and by a semicolon on Windows.
We set it with -cp (or -classpath or --class-path) on the java command, or with the CLASSPATH environment variable when no option is given. If neither is set, the class path is just the current directory. The JDK's own classes are not on the class path at all; they come from the JDK's modules through the bootstrap and platform loaders, which the application loader asks first.
2How the Search Works
When code asks for a class, the application loader delegates to its parents first, so JDK classes always win. If they do not have it, it walks the class path entries from left to right and stops at the first entry that contains the class file.
- Do the JDK loaders have it?YesUse the JDK classNoWalk the class path
- Entry 1: build/classes has com/acme/Order.class?YesLoad it, stop searchingNoNext entry
- Entry 2: lib/a.jar has it?YesLoad it, stop searchingNoNext entry
- No entry has it
ClassNotFoundExceptionorNoClassDefFoundError
4 steps in order. Play them, or step through them one at a time.
Notice that the search is per class, not per jar. If the newer jar lacks a class that the older one has, that class comes from the older jar, so one program can end up mixing classes from two versions of the same library. -verbose:class, or its newer form -Xlog:class+load, prints the source of every loaded class and is the quickest way to see which jar actually won.
3Wildcards: lib/*
Listing hundreds of jars by hand is impractical, so the java launcher accepts a wildcard entry: a directory name followed by an asterisk.
There are three rules worth remembering. The wildcard matches only jar files directly in the directory, not subdirectories and not class files. It must be the whole entry, so lib/*.jar is not a wildcard and is taken literally. And the order of the expanded jars is not guaranteed, it can differ between file systems and machines. We quote the entry so the shell does not expand it itself, because the shell would separate the jars with spaces, which is wrong for -cp. The unspecified order matters for the duplicate class problem: if two jars in lib contain the same class, which one wins can change between environments.
4Java -jar and the Manifest
When we run java -jar app.jar, the launcher ignores both -cp and the CLASSPATH variable. Instead, it reads META-INF/MANIFEST.MF inside the jar.
Main-Class names the class whose main method runs. Class-Path is a space-separated list of relative URLs, resolved against the location of the jar itself, not against the current directory, and the jar plus those entries become the class path. This is why a jar built this way works only when its lib folder is shipped next to it. Spring Boot executable jars use a different scheme: their Main-Class is Boot's launcher, which builds a class loader over the jars nested inside BOOT-INF/lib.
5When Two Jars Contain the Same Class
Because the first entry wins and the loader never looks further, duplicates are silent. Nothing warns that a second copy exists. The problem surfaces only when code compiled against one version calls something that exists only there.
Duplicates arise from two versions of one library, from a library and a shaded copy that did not relocate its packages, or from different artifacts that ship the same classes, such as two jars both containing the javax.annotation types. The fix is to have one copy: align versions in the build and use a duplicate class check, rather than relying on order.
6The Module Path Is Different
Since Java 9, jars can also go on the module path with --module-path. There, each jar is a module, either a named module with a module-info.class or an automatic module derived from its file name. The module system resolves modules at start-up and checks that each package belongs to exactly one module. If two named modules contain the same package, a split package, the JVM refuses to start with an error naming both modules, instead of silently picking one. Code on the class path forms the unnamed module, which can read everything but cannot override a package that a named module already owns. So the module path turns the class path's silent first-wins rule into an explicit failure at start-up.
7Follow-up questions
Why does the IDE run fine while the command line fails?
Because the IDE builds the class path from the project model, including directories such as resources and test outputs, and in its own order. The command line uses only what we pass, or the manifest with java -jar. Printing the effective class path with System.getProperty("java.class.path") in both environments usually shows the difference.
Does CLASSPATH apply with -cp?
No. The CLASSPATH environment variable is used only when no -cp, -classpath or --class-path option is given, and it is ignored entirely with java -jar. Relying on it is fragile, because it depends on the shell of whoever starts the program.
Can I see the class path inside a running JVM?
Yes. jcmd <pid> VM.system_properties shows java.class.path, and jcmd <pid> VM.command_line shows the options the JVM was started with, including -cp.
8Conclusion
In this answer, we saw that the application class loader searches the class path from left to right, directories and jars alike, and loads the first copy of each class it finds, after the JDK loaders have had their turn. A lib/* wildcard adds every jar directly in that directory in an unspecified order, java -jar ignores -cp and uses the manifest's Main-Class and Class-Path, and duplicates are silent because the first one wins. The module path behaves differently and refuses split packages between named modules at start-up.
Open with
The application class loader, after letting the JDK loaders try first, walks the class path entries from left to right. Each entry is a directory whose subfolders match the packages, or a jar with the same layout inside, and the first entry that contains the class file wins.
Then, if they want more
It is per class, so one program can even mix classes from two versions of a library. I set it with -cp, or the CLASSPATH variable if no option is given.
A wildcard like lib star, quoted so the shell does not expand it, adds every jar directly in lib, but not subdirectories and not loose class files, and the order of those jars is not specified, so it can differ between machines. With java -jar, both -cp and CLASSPATH are ignored.
The launcher reads the jar's manifest: Main-Class names the entry point, and Class-Path is a space-separated list of relative paths resolved against the jar's own location.
When two jars contain the same class, the first one found is used silently, and you usually find out later with a NoSuchMethodError or odd behaviour, because the code was compiled against the other version. -verbose:class shows which jar each class came from.
The module path from Java 9 is stricter: each package must belong to one module, so if two named modules contain the same package the JVM refuses to start, instead of silently picking one.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How does the JVM find classes on the classpath? Explain -cp with directories, jars and...": https://scrivio-interview-library.netlify.app/java/class-loading/e83c627cc0/