Scrivio Interview library
DebuggingIntermediate8 min read · updated 12 November 2026

A Spring Boot fat jar starts fine with java -jar app.jar, but an operations script that runs java -cp app.jar com.acme.Application fails with ClassNotFoundException for Spring classes. Another service never exits after main returns. Explain both problems.

The first script fails because a Spring Boot executable jar is not a normal jar: the dependencies sit inside it as nested jars under BOOT-INF/lib, and only Spring Boot's own launcher class, named in the manifest as Main-Class, knows how to load them. The plain java -cp class path cannot see nested jars. The second service never exits because the JVM only stops when every non-daemon thread has finished, and some executor or client thread is still alive after main returns.

Why this question is asked

Both problems look like Java being stubborn, but each one comes from a rule that is easy to miss. A job that never finishes keeps a scheduler waiting or piles up copies. Interviewers ask this because it checks whether we understand how a program actually starts and stops on the JVM, not just how to write main. In this answer, we will look inside a Spring Boot jar, see why the class path cannot read it, go through the three correct ways to start it, and then find and fix the thread that keeps the second JVM alive.

1A Quick Recap of How Java Starts a Program

When we run java -cp something com.acme.Application, the JVM creates the application class loader, the object that finds .class files, from the class path we gave it. A class path entry can be a directory or a jar file. For a jar, the class loader reads the entries at the top level of the zip, so a class com.acme.Application must be stored as com/acme/Application.class right at the root of the jar.

When we run java -jar app.jar instead, the JVM ignores any -cp option, opens the jar's META-INF/MANIFEST.MF file and reads its Main-Class attribute. That is the class it loads and whose main method it calls. Keep this difference in mind: -jar trusts the manifest, -cp trusts only the class path.

2What Is Inside a Spring Boot Executable Jar

A Spring Boot fat jar, the single jar that the Maven or Gradle plugin builds with everything inside, has its own layout. Our compiled classes go under BOOT-INF/classes. Every dependency, such as spring-core or jackson-databind, is copied whole as a jar file under BOOT-INF/lib. So the jar contains other jars, which is why we call them nested jars. At the root sits a small set of Spring Boot loader classes, and the manifest points at them.

Shell
$ unzip -p app.jar META-INF/MANIFEST.MF
Main-Class: org.springframework.boot.loader.launch.JarLauncher # Boot 3.2 and later
Start-Class: com.acme.Application # our class with main
$ unzip -l app.jar | grep -E "Application.class|spring-core"
BOOT-INF/classes/com/acme/Application.class # not at the root
BOOT-INF/lib/spring-core-6.2.x.jar # a jar inside the jar
Step throughHow java -jar starts a Boot application
javaJarLauncherNested class loaderApplication
read Main-Class, call main
create loader for BOOT-INF/classes and BOOT-INF/lib
read Start-Class: com.acme.Application
load through nested loader, call main(args)
load SpringApplication from spring-boot.jar
class found inside the nested jar

6 messages in order. Play them, or step through them one at a time.

The JVM only knows how to start JarLauncher; the launcher makes the nested jars visible.

Notice that Main-Class is not our class. It is JarLauncher, whose package moved from org.springframework.boot.loader to org.springframework.boot.loader.launch in Spring Boot 3.2. When java -jar starts it, JarLauncher builds a special class loader that can read classes directly out of the nested jars and out of BOOT-INF/classes. Then it reads Start-Class and calls com.acme.Application.main with that class loader in place. So the launcher does the work that the JVM itself cannot do.

3Why java -cp app.jar com.acme.Application Fails

Now the operations script makes sense. With -cp app.jar, the JVM's normal class loader looks only at the top-level entries of app.jar. It does not open jars inside jars; that is simply not a feature of the standard class path. So every class in BOOT-INF/lib is invisible, and the first reference to a Spring class fails with ClassNotFoundException or NoClassDefFoundError.

One detail is worth knowing. Our own Application class is under BOOT-INF/classes too, so with exactly this command the JVM often stops earlier with Could not find or load main class. If the script picks up the application classes from somewhere else, such as an extracted directory, the main class loads and the missing Spring classes are what we see. Either way, the cause is the same: the plain class path cannot read the nested layout.

4Three Correct Ways to Start the Jar

The simplest fix is to change the script to java -jar app.jar, so the manifest picks the launcher. If the script really needs -cp, for example to add an extra directory, it can start the launcher class itself, because the launcher classes sit at the root of the jar where a plain class path can find them.

Java
# 1. Let the manifest choose the launcher
java -jar app.jar # works
# 2. Start the launcher explicitly with a class path
java -cp app.jar org.springframework.boot.loader.launch.JarLauncher # works (Boot 3.2+ package)
# 3. Extract once, then run a normal jar with a lib/ folder
java -Djarmode=tools -jar app.jar extract --destination app # Boot 3.3+, check your version
java -jar app/app.jar # manifest Class-Path points at app/lib
Ways to start the same jar
CommandResultWhy
java -jar app.jarstartsmanifest Main-Class is JarLauncher
java -cp app.jar com.acme.Applicationfailsplain class path cannot read BOOT-INF/lib
java -cp app.jar ...launch.JarLauncherstartslauncher classes sit at the jar root
extract with jarmode=tools, java -jarstartsdependencies become ordinary jars in lib/

The third option is the one to choose for production containers and for scripts that want ordinary class path behaviour. The tools jar mode unpacks the nested jars into a plain lib directory and writes a small jar whose manifest lists them on its Class-Path. Older Boot versions offered java -Djarmode=layertools -jar app.jar extract instead, so check which mode your version supports.

5Why the Second Service Never Exits

The second problem has nothing to do with jars. The JVM exits on its own only when the last non-daemon thread finishes. A daemon thread is a background thread that does not keep the JVM alive; a non-daemon thread does. The main thread is non-daemon, and so are threads created by Executors.newFixedThreadPool and most client libraries, because the default thread factory creates non-daemon threads. Returning from main ends only the main thread.

Java
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(4);
pool.submit(() -> System.out.println("job done")); // job done
System.out.println("main returns"); // main returns
} // the JVM keeps running: four idle pool threads are still alive and non-daemon

The pool's worker threads finished the job, but they are not dead. They wait for the next task, and they will wait forever. In a Spring Boot batch service, SpringApplication.run returns a running context, and any scheduler, consumer or executor bean in it keeps its threads until the context is closed.

6Finding the Thread and Shutting Down Properly

To find the culprit, we take a thread dump of the hanging process with jcmd <pid> Thread.print or jstack <pid>. Each thread line shows whether it is a daemon; we look for non-daemon threads other than DestroyJavaVM, which is the main thread waiting for them. A name such as pool-1-thread-1 points to an unnamed executor.

The fix is to give every resource a lifecycle. An executor should be shut down when its work is done; since Java 19 ExecutorService is AutoCloseable, so try-with-resources waits for the tasks and then shuts it down. In Spring, beans that own threads should be closed by the context, and a job-style application should close the context and turn the result into an exit code.

Java
try (ExecutorService pool = Executors.newFixedThreadPool(4)) {
pool.submit(() -> System.out.println("job done")); // job done
} // close() waits for the task and shuts the pool down
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
int code = SpringApplication.exit(ctx); // closes the context, runs ExitCodeGenerator beans
System.exit(code); // 0 on success
}
}

Notice where System.exit lives: only in main, at the very edge of the application. Library code should never call System.exit, because it kills the whole JVM without letting the caller clean up, and it makes the code impossible to test or reuse inside a server. Instead, a library throws an exception or returns a result, and the application decides the exit code through its lifecycle, here SpringApplication.exit and ExitCodeGenerator beans. Spring also registers a shutdown hook, a thread the JVM runs when it receives SIGTERM, so the context is closed cleanly when a container is stopped.

7Follow-up questions

Why does Spring Boot's layered jar help container images?

A container image is built in layers, and Docker can reuse a layer from cache when its content has not changed. Spring Boot can split the extracted application into layers such as dependencies, spring-boot-loader, snapshot-dependencies and application, ordered from least to most often changed. When we change one line of our code, only the small application layer is rebuilt and pushed, while the large dependencies layer, often over 100 MB, is reused. Builds and deployments become faster and registries store less.

Could we mark the executor threads as daemon instead of shutting them down?

We could, with a ThreadFactory that calls setDaemon(true), and the JVM would then exit when main returns. But daemon threads are stopped abruptly at exit, in the middle of whatever they are doing, so a half-written file or an unsent message is lost. Daemon threads suit pure background helpers whose work does not matter at exit. For real work, an explicit shutdown that waits for tasks is the safer design.

8Conclusion

In this answer, we saw that a Spring Boot fat jar keeps its dependencies as nested jars under BOOT-INF/lib and relies on its JarLauncher, named as Main-Class, to load them before calling the Start-Class. A plain java -cp app.jar cannot read that layout, so we either use java -jar, start the launcher class explicitly, or extract the jar with the tools jar mode. We also saw that the JVM keeps running while any non-daemon thread is alive, how a thread dump finds that thread, and why shutdown belongs in the application's lifecycle with SpringApplication.exit rather than in System.exit calls scattered through library code.

What to say in the interviewAbout 90 seconds aloud

Open with

These are two separate problems. A Spring Boot executable jar is not a normal jar: our classes are under BOOT-INF/classes and every dependency is a nested jar under BOOT-INF/lib.

Then, if they want more

The manifest's Main-Class is Spring Boot's JarLauncher, which moved to the org.springframework.boot.loader.launch package in Boot 3.2. The launcher creates a class loader that can read nested jars and then calls the Start-Class, our application.

When the script runs java -cp app.jar com.acme.Application, it uses the plain class path, which only sees top-level entries, so the Spring classes, and usually even our own main class, cannot be found.

I would change the script to java -jar, run the JarLauncher class explicitly, or extract the jar with the tools jar mode and run it as a normal jar. The second service stays up because the JVM only exits when all non-daemon threads have ended, and returning from main ends only the main thread.

Usually an executor or client thread is still alive, or the Spring context was never closed. I would take a thread dump with jcmd pid Thread.print, look for non-daemon threads, and then shut that executor down properly or close the context.

For a job-style app I would call System.exit with SpringApplication.exit of the context in main, and keep System.exit out of library code entirely.

About this answer

Last updated
12 November 2026
Versions it names
Java 19, Spring Boot 3.2
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 "A Spring Boot fat jar starts fine with java -jar app.jar, but an operations script that...": https://scrivio-interview-library.netlify.app/java/classes-and-objects/b605b2fc86/