Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

A Spring Boot application packaged as an executable fat jar runs with java -jar app.jar. A third-party library calls ClassLoader.getSystemClassLoader().getResource("config/rules.xml") and gets null, although the file is in src/main/resources. The same code works when run from the IDE. Why, and how do you fix it?

In an executable Spring Boot jar, our classes and resources live under BOOT-INF/classes and dependency jars under BOOT-INF/lib, and they are loaded by a class loader that Boot's launcher creates at startup. The system class loader only sees the outer jar and the launcher's own classes, so getSystemClassLoader().getResource("config/rules.xml") returns null, while in the IDE everything is on the plain class path and it works. We fix it by having the library use the thread context class loader or the loader of an application class, or by passing the resource in explicitly; extracting the jar to a plain layout also avoids nested-jar loading.

Why this question is asked

Works in the IDE, fails in the packaged jar is one of the most common Spring Boot surprises, and it usually comes from a library that makes an assumption about class loaders. The file is obviously there, it is in src/main/resources, and yet the call returns null. Interviewers ask this to see whether we understand how an executable jar is built and started, and which class loader can see what. In this answer, we will look inside the fat jar, see what the system class loader can and cannot see, compare with the IDE, and then fix it in several ways.

1Inside an Executable Spring Boot Jar

A plain jar holds class files at its root, in package folders. Java's standard class loaders can read classes from such a jar, but they cannot read classes from a jar nested inside another jar. Spring Boot's executable jar, often called a fat jar, needs exactly that, because it packs every dependency as a jar inside the main jar. So it uses its own layout and its own launcher.

Shell
$ jar --list --file app.jar | head
META-INF/MANIFEST.MF
org/springframework/boot/loader/launch/JarLauncher.class <- Boot's launcher, at the root
BOOT-INF/classes/com/acme/App.class <- our classes
BOOT-INF/classes/config/rules.xml <- our resources
BOOT-INF/lib/rules-engine-2.4.jar <- dependencies, as nested jars
$ unzip -p app.jar META-INF/MANIFEST.MF
Main-Class: org.springframework.boot.loader.launch.JarLauncher
Start-Class: com.acme.App

Notice the manifest. java -jar app.jar does not start our App class; it starts JarLauncher, which sits at the root of the jar like normal classes. The launcher creates a special class loader that can read BOOT-INF/classes and every jar in BOOT-INF/lib, sets it as the thread context class loader, and then calls our App's main method through that loader. The launcher's package name changed in Spring Boot 3.2 to org.springframework.boot.loader.launch; older versions used org.springframework.boot.loader.

2What the System Class Loader Can See

The system class loader is the JVM's application loader. With java -jar app.jar, its class path is just app.jar itself, read as a plain jar. In that view, the only entries at the root are META-INF and the org/springframework/boot/loader classes. Our rules.xml is at BOOT-INF/classes/config/rules.xml, not at config/rules.xml, and the jars in BOOT-INF/lib are invisible because the standard loader does not open nested jars.

So when the library calls ClassLoader.getSystemClassLoader().getResource("config/rules.xml"), the system loader looks for config/rules.xml at the root of app.jar, finds nothing, and returns null. Meanwhile, all our classes and the library itself were loaded by Boot's launched class loader, a child of the system loader. Delegation goes from child to parent, never the other way, so the system loader cannot ask the child for help.

Java
// inside our application, running from java -jar app.jar
ClassLoader sys = ClassLoader.getSystemClassLoader();
ClassLoader app = App.class.getClassLoader();
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
System.out.println(sys.getResource("config/rules.xml")); // null
System.out.println(app.getResource("config/rules.xml")); // jar:nested:/app/app.jar/!BOOT-INF/classes/!/config/rules.xml
System.out.println(tccl == app); // true: the launcher set it
System.out.println(app.getParent() == sys); // true
Step throughClass loaders in a running fat jar
  1. Launched class loader (Boot)BOOT-INF/classes and BOOT-INF/lib/*.jar; also the context loader
  2. System (application) loaderapp.jar read as a plain jar: launcher classes only
  3. Platform loaderJDK modules
  4. Bootstrap loaderjava.base

4 layers, from the top down. Step through them one at a time.

The library asked the second layer for something only the top layer can see.

The exact URL format of nested resources depends on the Boot version, but the point is the same: the launched loader finds the file, and the system loader does not.

3Why It Works in the IDE

When the IDE runs the application, it does not build a fat jar. It starts java with a plain class path: the project's compiled classes directory, such as target/classes, which contains config/rules.xml at its root, and every dependency jar as a separate entry. Everything is loaded by the system class loader itself, so the library's call finds the file. The same happens with mvn spring-boot:run and in most tests. That is why the bug appears only when the packaged jar runs, often first in production.

4Fixing It

The cleanest fix is in the library: it should not assume the system class loader. Library code that loads application resources should use the thread context class loader, which every framework sets to the right loader, or the class loader of a class the caller provides, or better, accept the resource itself from the caller. If the library is ours, or the maintainers accept a patch, that is the change.

Java
// in the library, instead of ClassLoader.getSystemClassLoader()
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) cl = RulesEngine.class.getClassLoader();
URL rules = cl.getResource("config/rules.xml"); // found in the fat jar
// or let the application pass it in, using Spring's resource abstraction
@Bean RulesEngine rulesEngine(@Value("classpath:config/rules.xml") Resource rules) throws IOException {
try (InputStream in = rules.getInputStream()) {
return RulesEngine.fromXml(in); // no class loader assumption at all
}
}

If we cannot change the library, we look for a configuration option that accepts a file path, URL or stream, and pass it the resource from Spring. As a last resort, we can change how the application runs. Spring Boot 3.3 and later can extract the executable jar into a plain layout with java -Djarmode=tools -jar app.jar extract, which produces an application jar with normal classes and resources at its root and a lib folder referenced from its manifest, so a plain java -jar on the extracted jar puts everything on the system class path. That layout is also recommended for container images and needed for CDS archives, so it can be a reasonable choice in its own right; check the command for your Boot version.

5Follow-up questions

Why does Class.getResource("x") differ from ClassLoader.getResource("x") for a leading slash?

Class.getResource resolves a name without a leading slash relative to the class's package, so App.class.getResource("rules.xml") looks for com/acme/rules.xml, and a leading slash means from the root, so "/config/rules.xml" finds config/rules.xml. ClassLoader.getResource always resolves from the root and does not expect a leading slash: getResource("config/rules.xml") works, while getResource("/config/rules.xml") returns null. Mixing the two conventions is a common source of null resources.

Would getResourceAsStream on the system loader behave any differently?

No. It uses the same lookup, so it returns null for the same reason. The problem is which loader is asked, not which method.

Can we put the file somewhere the system loader can see it?

We could add it to the class path with the loader.path option of Boot's PropertiesLauncher or package it at the jar root, but these are workarounds that depend on packaging details. Fixing the class loader the library uses, or passing the resource explicitly, is more robust.

6Conclusion

In this answer, we saw that an executable Spring Boot jar keeps our classes and resources under BOOT-INF and loads them with a class loader created by Boot's launcher, so the system class loader cannot see them and returns null, while the IDE's plain class path hides the problem. The fixes are to use the thread context class loader or an application class's loader, to pass the resource in explicitly, or to run from an extracted plain layout.

What to say in the interviewAbout 90 seconds aloud

Open with

The difference is the class loader. In the IDE, the app runs with a plain class path, target/classes plus every dependency jar, so everything is loaded by the system class loader and it finds config/rules.xml.

Then, if they want more

In an executable Spring Boot jar, the manifest's Main-Class is Boot's JarLauncher, our classes and resources are under BOOT-INF/classes, and the dependencies are nested jars under BOOT-INF/lib.

Java's standard loaders cannot read nested jars, so the launcher creates its own class loader for all of that, sets it as the thread context class loader and starts our main class through it.

The system class loader only has app.jar as a plain jar, which contains the launcher classes and BOOT-INF, so looking for config/rules.xml at the root returns null. And because delegation goes upward only, it cannot ask the launched loader. To fix it, the library should use Thread.currentThread().getContextClassLoader(), or the loader of an application class, instead of the system loader.

Even better, the library accepts the resource, so in Spring I would inject a Resource for classpath:config/rules.xml and pass its stream.

If I cannot change the library at all, I can run the app from the extracted layout that java -Djarmode=tools -jar app.jar extract produces, where classes and resources are on the plain class path; that layout is good for containers and CDS anyway.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3.2, Spring Boot 3.3
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 application packaged as an executable fat jar runs with java -jar app.jar....": https://scrivio-interview-library.netlify.app/java/class-loading/7fc359c098/