Quick revision
The short answer
With java -cp app.jar com.acme.Main, the JVM puts the outer jar on the classpath and starts our class directly, so Boot's launcher never runs and nobody can read the nested jars in BOOT-INF/lib, which is where all the Spring classes are. java -jar works because it starts the launcher named in the manifest. We fix it by using java -jar, by naming the launcher class with -cp, by extracting the jar (for example with the tools jar mode) and running with a normal classpath, or by using PropertiesLauncher with loader.path when we need to add external jars.
What to remember
In this answer, we saw that java -cp app.jar com.acme.Main skips Boot's launcher, so the JVM's class loader sees only the outer jar and cannot read the nested jars in BOOT-INF/lib, and with a real Boot jar it cannot even find our own class in BOOT-INF/classes. java -jar works because it starts JarLauncher from the manifest. We fixed it by using java -jar, naming org.springframework.boot.loader.launch.JarLauncher with -cp, extracting the jar with the tools jar mode for a normal classpath, or using PropertiesLauncher with loader.path to add external jars.
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.
With java -cp app.jar com.acme.Main, the JVM puts the outer jar on the classpath and starts our class directly, so Boot's launcher never runs and nobody can read the nested jars in BOOT-INF/lib, which is where all the Spring classes are. java -jar works because it starts the launcher named in the manifest. We fix it by using java -jar, by naming the launcher class with -cp, by extracting the jar (for example with the tools jar mode) and running with a normal classpath, or by using PropertiesLauncher with loader.path when we need to add external jars.
Why this question is asked
This comes up when someone moves from java -jar to a custom start script, a process manager, or a container entrypoint that sets the classpath explicitly, often to add an agent or an extra jar. The application then fails immediately, and the stack trace points at Spring, which makes people suspect a dependency problem. Interviewers ask it because it shows whether we understand how a Boot jar is built and started. In this answer, we will recap the jar layout, reproduce the failure, explain exactly what the JVM can and cannot see, and go through four ways to fix it.
1A Quick Recap of the Jar Layout
A Spring Boot executable jar is not an ordinary jar. Our classes are in BOOT-INF/classes, every dependency is stored as a complete jar inside BOOT-INF/lib, and only Boot's loader classes sit at the root. The manifest's Main-Class is Boot's JarLauncher, and Start-Class names our application class. The launcher is the only piece of code that knows how to read classes from the nested jars.
2Reproducing the Failure
Here are the two commands side by side, and what each one prints.
Notice the detail. With a real Boot jar, the very first failure usually names our own main class, because it also lives under BOOT-INF/classes and not at the root, so the JVM cannot find it at com/acme/orders/OrdersApplication.class. When the error names Spring classes, as in the question, the classpath contains our classes at the root, for example the plain jar that Gradle builds next to the boot jar, or a script that adds the extracted BOOT-INF/classes directory, but the dependencies are still not there. Both cases have the same cause: no launcher, so no nested jars.
3Why the JVM Cannot See the Spring Classes
With -cp, the JVM's application class loader gets one entry: app.jar. It can load any .class file stored directly in that zip at the path matching its package. It cannot look into BOOT-INF/lib/spring-boot-3.5.x.jar, because that is a jar stored inside a jar, and the JDK has no built-in support for that. It also ignores the manifest's Main-Class and Start-Class, because those are only read when we use -jar.
With -jar, the JVM reads Main-Class, starts JarLauncher, and the launcher creates its own class loader with one URL for BOOT-INF/classes and one for each nested jar, through a URL handler that reads them in place. Our main method is then called through that loader, and every Spring class is found.
| Command | Launcher runs? | Sees our classes? | Sees BOOT-INF/lib? |
|---|---|---|---|
java -jar app.jar | yes | yes | yes |
java -cp app.jar com.acme.Main | no | no (BOOT-INF/classes) | no |
java -cp plain.jar com.acme.Main | no | yes | no |
java -cp app.jar ...launch.JarLauncher | yes | yes | yes |
| java -cp 'extracted/lib/*:extracted/app.jar' Main | not needed | yes | yes, as plain jars |
4Fix 1 and 2: Use java -jar, or Name the Launcher
The simplest fix is to go back to java -jar app.jar. JVM options still work in front of it, for example java -javaagent:/opt/agent.jar -Xmx768m -jar app.jar, so an agent is not a reason to switch to -cp. If a tool insists on a classpath and a main class, we give it the launcher as the main class.
This works because the launcher reads the manifest of the jar it was loaded from, finds Start-Class, and builds the nested class loader just as with -jar. Mind the package: scripts written for Boot 3.1 and earlier use the old name and fail after an upgrade.
5Fix 3: Extract the Jar and Use a Normal Classpath
If we want an ordinary classpath, for example to start faster or to use class data sharing, we unpack the jar into plain jars once, at build time, with the tools jar mode available since Boot 3.3.
The extracted app.jar is a normal jar: our classes at its root and a standard Class-Path in its manifest, so no Boot loader is involved at all. On older Boot versions, the equivalent was to unzip the jar and run with -cp "BOOT-INF/classes:BOOT-INF/lib/*" and the main class.
6Fix 4: PropertiesLauncher With loader.path
Sometimes the real reason for -cp was to add external jars, such as a JDBC driver or plugins that are not packaged in the application. Boot's PropertiesLauncher supports that: it builds its class path from the loader.path property, which can list directories and jars outside the application jar.
We can also make PropertiesLauncher the jar's default Main-Class by setting the layout to ZIP in the Maven or Gradle plugin, and then pass -Dloader.path with java -jar. This keeps the nested jar loading and adds the external entries, so it is the right tool when the application must pick up jars that are deployed separately.
7Follow-up questions
Why does the Gradle build produce two jars?
Gradle's bootJar task builds the executable jar, and the plain jar task still builds an ordinary jar named with a -plain suffix that contains only our classes. Running the plain jar with -cp gives exactly the Spring ClassNotFoundException from the question. We can disable the plain jar with jar { enabled = false } so nobody picks it up by mistake.
Can we add a Java agent without -cp?
Yes. -javaagent is a JVM option, independent of how the main class is found, so java -javaagent:/opt/otel.jar -jar app.jar works. Agents are loaded by the system class loader before the launcher starts, which is what they expect.
Does the same apply in a Dockerfile?
Yes. An ENTRYPOINT that runs java -cp /app/app.jar com.acme.Main fails the same way. We use java -jar, the launcher class, or an extracted layout. Recent Boot documentation recommends extracting with the tools jar mode in the image build and running java -jar on the extracted application jar.
8Conclusion
In this answer, we saw that java -cp app.jar com.acme.Main skips Boot's launcher, so the JVM's class loader sees only the outer jar and cannot read the nested jars in BOOT-INF/lib, and with a real Boot jar it cannot even find our own class in BOOT-INF/classes. java -jar works because it starts JarLauncher from the manifest. We fixed it by using java -jar, naming org.springframework.boot.loader.launch.JarLauncher with -cp, extracting the jar with the tools jar mode for a normal classpath, or using PropertiesLauncher with loader.path to add external jars.
Open with
The difference is who starts the application. With java -jar, the JVM reads the manifest, starts Boot's JarLauncher, and the launcher builds a class loader that can read BOOT-INF/classes and every nested jar in BOOT-INF/lib, then calls our main method.
Then, if they want more
With java -cp app.jar com.acme.Main, the manifest is ignored, the launcher never runs, and the JVM's normal class loader only sees what is stored directly at the root of the zip. It cannot read jars inside a jar, so none of the Spring classes are visible.
Strictly, with a real Boot jar the first error usually names our own main class, because it also sits under BOOT-INF/classes; if the error names Spring classes, the classpath probably has the plain jar or the extracted classes directory, but still no dependencies.
To fix it, the simplest option is java -jar, and JVM options like -javaagent still work in front of it. If a tool needs -cp, I name the launcher as the main class, org.springframework.boot.loader.launch.JarLauncher since Boot 3.2. If I want a plain classpath, I extract the jar with java -Djarmode=tools -jar app.jar extract and run the extracted app.jar. And if the goal was adding external jars, I use PropertiesLauncher with loader.path.
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 "An application started with java -cp app.jar com.acme.Main fails with...": https://scrivio-interview-library.netlify.app/spring-boot/packaging-and-deployment/3d2e313c7b/