Quick revision
The short answer
The exception means two different classes both named com.acme.Order exist in the JVM, because they were loaded by two different class loaders: DevTools' restart class loader and the base class loader. It usually happens when a library in the base loader deserialises or caches objects and creates them from its own class loader, or keeps objects from before a restart. We fix it by moving the affected jars into the restart loader with restart.include entries in META-INF/spring-devtools.properties, by changing the library's behaviour, or, as a last resort, by disabling restart.
What to remember
In this answer, we saw that the JVM identifies a class by its name and its class loader, and that DevTools runs our classes in a restart class loader on top of a base loader for jars. A ClassCastException between two classes with the same name means an object was created from the other loader's copy, usually by a library that deserialises or caches with the base loader, or by state that survived a restart. We fixed it by moving the jar into the restart loader with restart.include in META-INF/spring-devtools.properties, by changing the library's class loader or caching behaviour, or by disabling restart.
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 exception means two different classes both named com.acme.Order exist in the JVM, because they were loaded by two different class loaders: DevTools' restart class loader and the base class loader. It usually happens when a library in the base loader deserialises or caches objects and creates them from its own class loader, or keeps objects from before a restart. We fix it by moving the affected jars into the restart loader with restart.include entries in META-INF/spring-devtools.properties, by changing the library's behaviour, or, as a last resort, by disabling restart.
Why this question is asked
Few error messages look as absurd as "Order cannot be cast to Order". It is a favourite interview question because it cannot be solved by reading the code; the code is fine. It tests whether we know that the JVM identifies a class by its name and its class loader together, and how DevTools uses class loaders. In this answer, we will recap class identity in the JVM, reproduce the problem, see where the second copy of the class comes from, and then go through the fixes from the most targeted to the bluntest.
1A Quick Recap: What Makes Two Classes the Same
A class loader is the component that reads bytecode and defines a Class object from it. In the JVM, a class's identity is the pair of its fully qualified name and its defining class loader. Two class loaders can each load com/acme/Order.class from the same file, and the JVM will treat the results as two unrelated types, just as if they had different names.
A cast checks that identity. When we write (Order) obj, the JVM checks whether obj's class is the Order class that our code was linked against. If obj was created from the other copy, the cast fails, and the message prints both names, which happen to be identical.
2Why DevTools Creates Two Class Loaders
With DevTools on the classpath, the application runs with a base class loader for dependency jars and a restart class loader for our project's own classes in target/classes. Our code runs in the restart loader, so when our code says Order, it means the Order defined by the restart loader.
After each restart, DevTools throws the old restart loader away and creates a new one. So over a development session there may be a sequence of Order classes: one per restart loader. Normally the old ones disappear with their loader. The trouble starts when something in the base loader either loads Order itself or keeps an object alive across a restart.
| Who created the object | Class loader of its class | Can our code cast it to Order? |
|---|---|---|
Our controller via new Order() | current RestartClassLoader | yes |
| A cache entry from before the restart | old RestartClassLoader | no: ClassCastException |
| A library deserialising with its own loader | base AppClassLoader | no: ClassCastException |
3Reproducing the Problem
A typical case is a cache or a serialisation library that lives in the base class loader. Suppose we store orders in a local cache that serialises entries, and the library resolves class names with its own class loader. Our code puts an Order in, then reads it back.
Notice that recent JVMs print the class loader of each class in the message. That detail is the quickest way to recognise the problem: the names match, the loaders do not.
4Where the Second Copy Comes From
There are two common sources. The first is deserialisation. Java serialisation, and some caching and messaging libraries, turn bytes back into objects by looking up the class by name. If they use the class loader that loaded the library, the base loader, it can find com/acme/Order.class on the original classpath and define a second Order there. A well-behaved library uses the thread context class loader, which DevTools sets to the restart loader, but not every library does.
The second is long-lived state. A cache, a static registry or a session store in the base loader holds an Order created before the last restart. After the restart, our new code has a new Order class, and the old object belongs to the discarded one. The same applies to classes from another module of our own project that arrives as a jar, for example a shared domain module in a multi-module build: that jar lands in the base loader, while code that uses it is in the restart loader.
5Fix 1: Move the Jar Into the Restart Class Loader
The targeted fix is to tell DevTools that a jar belongs with our code, so it is loaded by the restart loader and reloaded with it. We do this with a file named META-INF/spring-devtools.properties on the classpath, with restart.include entries whose values are regular expressions matched against jar names. There is also restart.exclude for the opposite direction.
Now the library and our classes share the restart loader, so the library resolves Order to the same class our code uses, and its cache is rebuilt on each restart instead of holding stale objects. Note that a properties file treats a backslash as an escape character, which is why every backslash of the regular expression is doubled in the file; the pattern DevTools actually matches is /acme-domain-[\w\d-\.]+\.jar.
6Fix 2 and Fix 3: Change the Behaviour or Disable Restart
If moving jars is not possible, we can change the library's behaviour. Many libraries let us pass a class loader for deserialisation, and giving them Thread.currentThread().getContextClassLoader() or our own class's loader fixes the lookup. For caches, we can disable the cache in development, use an in-process cache that stores references instead of serialised copies, or clear it on restart.
If neither works, we can disable restart while keeping LiveReload and the other DevTools features. It must be set before the application starts, as a system property, because the restart decision happens at the very beginning of run.
7Follow-up questions
Why does the error often appear only after the first restart?
Before the first restart, the cached objects and our code may happen to use the same Order class, for example when the cache was filled by our code and stores references. After a restart, our code uses a new Order class from a new restart loader, while the cache still holds objects of the old one. That is a strong hint that long-lived state in the base loader is the cause.
Can this happen without DevTools?
Yes. Any setup with several class loaders can produce it: application servers with multiple deployments, plugin systems, OSGi, or tests that create custom class loaders. DevTools is simply the most common place Spring developers meet it.
How do you confirm which loaders are involved?
Log obj.getClass().getClassLoader() and Order.class.getClassLoader() at the point of the cast. If one is RestartClassLoader and the other is the application class loader or an older RestartClassLoader instance, the diagnosis is confirmed and we know which jar needs a restart.include entry.
8Conclusion
In this answer, we saw that the JVM identifies a class by its name and its class loader, and that DevTools runs our classes in a restart class loader on top of a base loader for jars. A ClassCastException between two classes with the same name means an object was created from the other loader's copy, usually by a library that deserialises or caches with the base loader, or by state that survived a restart. We fixed it by moving the jar into the restart loader with restart.include in META-INF/spring-devtools.properties, by changing the library's class loader or caching behaviour, or by disabling restart.
Open with
The message looks impossible, but it means there are two classes called com.acme.Order in the JVM. A class's identity is its name plus the class loader that defined it, and DevTools uses two loaders: a base loader for dependency jars and a restart loader for our classes, which it replaces on every restart.
Then, if they want more
Our code casts to the restart loader's Order, but the object came from the other one. Usually that is a library in the base loader, a cache or a deserialiser, that resolves the class by name with its own loader, or a cache that kept an object from before the last restart.
A shared module of ours that arrives as a jar can do the same. I confirm it by logging the class loader of the object and of Order.class; recent JVMs even print both loaders in the exception.
The fix I try first is putting the jar into the restart loader with a restart.include entry in META-INF/spring-devtools.properties, a regex on the jar name, so the library and our code share a loader and its state is rebuilt on restart.
If that is not possible, I make the library use the context class loader or turn off that cache in development. As a last resort I disable restart with the spring.devtools.restart.enabled system property before run.
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.
- Official documentation
- Spring Boot reference
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 "With DevTools on the classpath, a ClassCastException says com.acme.Order cannot be cast...": https://scrivio-interview-library.netlify.app/spring-boot/developer-experience/fed8816d21/