Quick revision
The short answer
Tomcat searches a web application's own WEB-INF/classes and WEB-INF/lib before the shared common loader, so each application can ship its own library versions without the container's copies winning. It still loads JDK classes and the Servlet API from the parent first and refuses to let an application override them. The classic bug is a library present both in the container's lib and in the WAR: two copies of a class exist, and objects that cross between them fail with ClassCastException or a loader constraint LinkageError.
What to remember
In this answer, we saw that Tomcat searches a web application's own classes before the shared common loader so that each WAR can ship its own library versions, while still taking JDK classes and the Servlet API from the parent and filtering any attempt to override them. We reproduced the main risk, a library in both the container's lib and the WAR, which creates two types with one name and leads to ClassCastException or loader constraint errors. Setting delegate to true restores parent first, and Spring Boot's one-application-per-JVM jars avoid the problem by design.
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.
Tomcat searches a web application's own WEB-INF/classes and WEB-INF/lib before the shared common loader, so each application can ship its own library versions without the container's copies winning. It still loads JDK classes and the Servlet API from the parent first and refuses to let an application override them. The classic bug is a library present both in the container's lib and in the WAR: two copies of a class exist, and objects that cross between them fail with ClassCastException or a loader constraint LinkageError.
Why this question is asked
Most Java developers learn that class loading is parent first, and then meet a servlet container that does the opposite. The difference is not a curiosity: it is the reason a WAR can bundle Jackson 2.17 while the server ships 2.15, and it is also the reason for some of the most confusing errors we see, such as a class that cannot be cast to itself. Interviewers ask this to check that we understand both sides. In this answer, we will recap the normal rule, see why containers turn it around, follow Tomcat's actual search order, reproduce the two-copies bug, and look at what the delegate setting and Spring Boot's executable jars change.
1A Quick Recap of Parent-First Delegation
A class loader turns a class name into a loaded class. Loaders form a tree, and the standard rule is parent first: when asked for a class, a loader asks its parent before looking in its own files. That gives every class in the JVM the same copy of shared types such as java.lang.String.
A servlet container such as Tomcat hosts several web applications in one JVM. Its loaders are arranged like this: the JDK's bootstrap and platform loaders, then the system loader with Tomcat's startup classes, then the common loader with everything in Tomcat's lib directory (the Servlet API, Tomcat itself, and anything an administrator dropped there), and finally one web application loader per deployed WAR, reading that WAR's WEB-INF/classes and WEB-INF/lib.
2Why Containers Load the Application First
Imagine Tomcat's lib directory contains a logging library at version 1, and our WAR bundles version 2 because we need a newer method. With pure parent first, the common loader would find version 1 and our application would silently run against it, failing at run time with NoSuchMethodError the first time it calls the newer method. Worse, every application on the server would be stuck with whatever the administrator installed.
The Servlet specification therefore recommends that a web application's own classes take priority, and Tomcat does this by default. Each application can bundle exactly the versions it was built and tested with, and two applications on the same server can use different versions of the same library, because each version lives in a different web application loader.
3Tomcat's Actual Search Order
Child first does not mean the application can replace anything. Tomcat's web application loader runs these checks in order when asked for a class.
- Already loaded by this loader?YesReturn itNoContinue
- Found by the JDK's own loaders?java.*, javax.* JDK classesYesReturn the JDK classNoContinue
- Is it a filtered package?
jakarta.servlet, EL API, Tomcat internalsYesLoad from the parent onlyNoContinue - Search WEB-INF/classes, then WEB-INF/libthe application's own copies win
- Fall back to system and common loadersshared drivers in Tomcat's lib
5 steps in order. Play them, or step through them one at a time.
So JDK classes always come from the JDK, because Tomcat consults the JDK's own loaders before the web application's files. Then Tomcat applies a filter: classes in packages such as jakarta.servlet (javax.servlet in older versions), the expression language API and Tomcat's own internals are always delegated to the parent, and copies found in WEB-INF/lib are ignored, with a log message for jars that contain the Servlet API. The reason is that the container and the application must agree on what a Servlet, a Filter and an HttpServletRequest are; if the WAR defined its own Filter interface, Tomcat could never call it.
Only after those checks does Tomcat search WEB-INF/classes, then WEB-INF/lib, and finally fall back to the system and common loaders. That last fallback is what lets an application use a shared JDBC driver installed in Tomcat's lib directory.
4The Bug: Two Copies of the Same Class
Now the catch. Suppose an administrator puts acme-model.jar in Tomcat's lib so a shared component can use it, and our WAR also bundles acme-model.jar in WEB-INF/lib. Our application's classes load com.acme.Order from WEB-INF/lib, but the shared component, loaded by the common loader, sees the common loader's copy. The JVM now has two different types called com.acme.Order, because a class's identity is its name plus its defining loader.
The message looks absurd until we read the loader names at the end: the two classes have the same name but different loaders. A subtler version is a loader constraint violation, a LinkageError the JVM raises when it notices that a method signature linking two loaders would refer to two different copies of the same class. Both errors happen only when an object crosses the boundary, which is why they often appear in one code path and not another.
The fix is to keep each library in exactly one place. Shared libraries that pass objects between the container and the applications belong only in the container; everything else belongs only in the WAR.
5What the delegate Attribute Changes
Tomcat lets us switch an application back to standard behaviour with the delegate attribute of the Loader element in the application's context.xml.
With delegate set to true, the parent loaders are searched before the application's own files, so a class in Tomcat's lib wins over the WAR's copy. That removes the version freedom child first gave us, but it also means the container's copy is always used and there is never a second copy to clash with. It is a reasonable choice when the server deliberately provides a set of shared libraries that applications must use.
6Why Spring Boot Jars Rarely Have This Problem
A Spring Boot executable jar runs one application in its own JVM. Its launcher creates a class loader that reads BOOT-INF/classes and BOOT-INF/lib and follows normal parent-first delegation, and the embedded Tomcat is just another library inside the same loader. There is no shared container lib directory and no second application, so the two-copies situation from a shared server mostly disappears. We can still create it ourselves by building our own class loaders, but it no longer happens by default.
7Follow-up questions
What changes if you set the Tomcat Loader delegate attribute to true?
The web application loader becomes parent first: after the JDK classes, it searches the system and common loaders before WEB-INF/classes and WEB-INF/lib. Libraries in Tomcat's lib then override the WAR's own copies, so the application loses the ability to choose its own versions, but duplicate classes across the boundary can no longer occur because the parent copy always wins.
Why does Tomcat ignore a servlet-api jar inside WEB-INF/lib?
Because the container calls the application through Servlet API types such as Filter and HttpServletRequest. If the application defined its own copy of those interfaces, the container's objects would not be instances of the application's types and nothing would work. Tomcat therefore always delegates those packages to the parent and logs that it skipped the jar.
How do you find which loader defined a class at run time?
Call getClass().getClassLoader() on the object, or print SomeClass.class.getProtectionDomain().getCodeSource().getLocation() to see the jar it came from. The ClassCastException message in modern JDKs also names both loaders, which usually points straight to the duplicate jar.
8Conclusion
In this answer, we saw that Tomcat searches a web application's own classes before the shared common loader so that each WAR can ship its own library versions, while still taking JDK classes and the Servlet API from the parent and filtering any attempt to override them. We reproduced the main risk, a library in both the container's lib and the WAR, which creates two types with one name and leads to ClassCastException or loader constraint errors. Setting delegate to true restores parent first, and Spring Boot's one-application-per-JVM jars avoid the problem by design.
Open with
Tomcat loads web application classes child first so that each WAR can bundle its own library versions. If it were parent first, a library an administrator placed in Tomcat's lib would override the application's copy, and the application would fail at run time with errors like NoSuchMethodError.
Then, if they want more
It is not completely child first though. The web application loader first lets the JDK's loaders answer, so java classes always come from the JDK, and it filters the Servlet API, the EL API and Tomcat's internals so they always come from the parent. The container and the application must agree on what a Filter or a request is.
After that it searches WEB-INF/classes, then WEB-INF/lib, and only then the system and common loaders. The bug this causes is two copies of one class.
If a jar is in both Tomcat's lib and the WAR, and an object created by one copy reaches code compiled against the other, you get ClassCastException saying a class cannot be cast to itself, with two loader names in the message, or a loader constraint LinkageError. The fix is to keep each library in one place.
Setting delegate to true on the Loader makes it parent first again. Spring Boot executable jars run one application per JVM with normal delegation, so this is mostly a shared-container problem.
About this answer
- Last updated
- 12 November 2026
- 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 "Servlet containers such as Tomcat load web application classes child-first...": https://scrivio-interview-library.netlify.app/java/class-loading/2560678bdd/