Quick revision
The short answer
When a class is found in the IDE but not in the packaged jar, the two are not using the same classpath. The usual causes are that the IDE and the packaging step resolve different versions of the library, that a dependency is in provided or test scope and so is left out of the jar, or that two artifacts contain the same packages and the wrong one ends up packaged. We find out by listing BOOT-INF/lib in the jar and comparing it with dependency:tree or dependencyInsight, and we fix it by aligning versions in dependencyManagement or a platform and adding a duplicate class check.
What to remember
In this answer, we saw that a class that works in the IDE but is missing from the packaged jar means the two classpaths differ. The three usual causes are different resolved versions because of profiles, configurations or conflict resolution, a dependency in provided or test scope that is left out of the jar, and two artifacts with the same packages where the wrong one wins. We inspect BOOT-INF/lib, compare it with dependency:tree or dependencyInsight run the way CI runs it, fix the version in dependencyManagement or a platform, and add convergence and duplicate class checks plus a smoke test of the real jar.
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.
When a class is found in the IDE but not in the packaged jar, the two are not using the same classpath. The usual causes are that the IDE and the packaging step resolve different versions of the library, that a dependency is in provided or test scope and so is left out of the jar, or that two artifacts contain the same packages and the wrong one ends up packaged. We find out by listing BOOT-INF/lib in the jar and comparing it with dependency:tree or dependencyInsight, and we fix it by aligning versions in dependencyManagement or a platform and adding a duplicate class check.
Why this question is asked
This failure is frustrating because the code is clearly fine: it compiles, the tests pass and the application starts in the IDE. Then the jar we deploy fails on the first request. Interviewers ask about it because the answer shows whether we understand that a classpath is something the build computes, and that the IDE, the test run and the packaged jar can each compute it differently. In this answer, we will recap how a ClassNotFoundException happens, go through the likely causes, see how to inspect the jar and compare it with the dependency tree, and then fix the problem so it cannot quietly return.
1A Quick Recap: What ClassNotFoundException Means
The JVM loads classes lazily. When code first needs a class, a class loader looks for it on the classpath, the list of jars and folders the application was started with. If no entry contains the class file, we get ClassNotFoundException (when the name is looked up by reflection or a framework) or NoClassDefFoundError (when compiled code refers to it directly). Both mean the same thing for us: the class was not on the classpath at runtime.
The question gives an important clue: the class exists in another version of the same library. Libraries add, move and remove classes between versions. If our code, or another library we use, was compiled against version 2.15 and the jar contains version 2.9, a class introduced in 2.12 simply is not there. So the real question is: why does the packaged jar contain a different version, or no copy at all, compared with what the IDE uses?
2Cause 1: The IDE and the Jar Resolve Different Versions
The IDE imports the project's dependencies once, from its own view of the build. The packaged jar is produced by a separate run of Maven or Gradle, often on a CI agent. These can disagree for several ordinary reasons. A Maven profile that is active in the IDE (or on a developer's settings.xml) may add or override a dependency that the CI profile does not. The IDE may have resolved the project a week ago, before someone changed a version, and never re-synced. In Gradle, a different configuration is involved: the IDE may show compileClasspath while the jar is built from runtimeClasspath, and the two can resolve different versions when a dependency is declared differently in each.
Version conflict resolution matters here as well. Maven picks the nearest declaration in the dependency tree, and if two declarations are at the same depth, the first one found. Gradle picks the highest version requested. Adding a new starter can change which version wins without anyone touching the library itself.
3Cause 2: A Provided or Test Scope Dependency
Scopes decide where a dependency is available. A dependency in test scope is on the classpath for tests only. A dependency in provided scope (compileOnly in Gradle) is available for compiling, but the build assumes the runtime environment will supply it, so it is left out of the packaged application. IntelliJ, when it runs a main class, may include provided dependencies on the classpath for convenience. So the application starts in the IDE, but the executable jar built by spring-boot-maven-plugin does not contain the jar, and the class is missing.
This is a common leftover from moving a WAR, where provided made sense, to an executable Spring Boot jar.
4Cause 3: Two Artifacts With the Same Packages
Sometimes a library is renamed or split, and its classes move to a new artifact while keeping, or changing, their packages. A classic example is the move from javax to jakarta: an old javax.servlet or javax.persistence artifact and the newer jakarta artifact can both be on the classpath. Another is a library that publishes an all-in-one jar and separate module jars that contain the same classes. When two jars contain classes in the same package, the class loader uses the first one it finds on the classpath, and the order can differ between the IDE and the packaged jar. If the first one is the older copy, which lacks the class we need, the class is not found even though the newer copy is in the jar too.
This cause is hard to see in a dependency tree, because the two artifacts have different names, so the tree shows no conflict at all.
5Inspecting the Jar and Comparing With the Tree
The packaged jar is the truth, so we start there. A Spring Boot executable jar keeps its dependencies under BOOT-INF/lib. We list them and look for the library, then compare with what the build tool says it resolved.
- List BOOT-INF/lib in the packaged jarthe jar is the truth, not the IDE
- Is the library jar there at all?Yescheck its version and duplicatesNolook for provided, test or compileOnly scope
- Is it the version the IDE uses?Yeslook for a second jar with the same packagesNocompare dependency:tree or dependencyInsight with CI's profiles
- Align versions, fix the scope or exclude the duplicatethen add a check so it fails the build next time
4 steps in order. Play them, or step through them one at a time.
The listing tells us which of the three causes we have. A wrong version points to cause 1, a missing jar points to cause 2, and two different jars that both contain the package point to cause 3. We should run the dependency commands from the command line exactly as CI does, with the same profiles, rather than trust the IDE's dependency view.
6Fixing It So It Stays Fixed
For a version mismatch, we declare the version we need in one place: dependencyManagement in Maven, or a platform (a BOM, a bill of materials that fixes versions for a family of libraries) in Gradle. With Spring Boot, which already manages Jackson, the fix is often just removing an old explicit version. For a scope problem, we change provided to compile (or compileOnly to implementation). For duplicate packages, we exclude the old artifact from the dependency that brings it in.
Then we make the build catch it next time. Maven Enforcer has rules such as dependencyConvergence, which fails when two paths request different versions, and the extra-enforcer-rules project adds banDuplicateClasses, which fails when two jars contain the same class. In Gradle, failOnVersionConflict() in the resolution strategy, or a duplicate class checking plugin, does the same job. Finally, a smoke test that starts the packaged jar in CI and calls one endpoint catches anything the static checks miss, because it tests the artifact we ship rather than the IDE classpath.
7Follow-up questions
What is the difference between ClassNotFoundException and NoClassDefFoundError here?
ClassNotFoundException is thrown when code asks for a class by name, for example through Class.forName or a framework scanning configuration, and no class loader finds it. NoClassDefFoundError is thrown when a class that was present at compile time and is referenced directly by compiled code cannot be loaded at runtime. In this scenario both point to the same root cause, a runtime classpath that differs from the compile classpath, and we investigate them the same way.
Why does the order of jars matter if both are on the classpath?
Because the class loader returns the first class file it finds for a given name and never looks further. If two jars contain com.example.Foo and the older one comes first, every caller gets the older class. When the newer jar has extra classes in the same package, those are still found, which leads to confusing mixtures of old and new code, so the real fix is to have only one jar for each package.
8Conclusion
In this answer, we saw that a class that works in the IDE but is missing from the packaged jar means the two classpaths differ. The three usual causes are different resolved versions because of profiles, configurations or conflict resolution, a dependency in provided or test scope that is left out of the jar, and two artifacts with the same packages where the wrong one wins. We inspect BOOT-INF/lib, compare it with dependency:tree or dependencyInsight run the way CI runs it, fix the version in dependencyManagement or a platform, and add convergence and duplicate class checks plus a smoke test of the real jar.
Open with
The jar and the IDE are not using the same classpath, so I would start from the jar, because that is what runs in production. I would list BOOT-INF/lib and check whether the library is there and in which version.
Then, if they want more
If it is missing, the usual cause is a provided or test scope, or compileOnly in Gradle, which the IDE may still include when it runs the app.
If it is there but older than in the IDE, the build resolved a different version, maybe because of a profile, because the IDE was not re-synced, or because Maven's nearest-wins rule picked a transitive copy. I would confirm with mvn dependency:tree with the same profiles as CI, or Gradle dependencyInsight on runtimeClasspath.
A third cause is two artifacts carrying the same packages, for example an old javax jar next to its jakarta successor, where the first one on the classpath wins.
To fix it I would align the version in dependencyManagement or a platform BOM, correct the scope or exclude the duplicate, and then add Maven Enforcer's dependencyConvergence and banDuplicateClasses or Gradle's failOnVersionConflict, plus a CI smoke test that starts the packaged jar.
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
- Apache Maven guides, Gradle user manual
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 "A service works in the IDE but fails in the packaged jar with ClassNotFoundException for...": https://scrivio-interview-library.netlify.app/java/dependency-and-supply-chain/34552dcec0/