Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

A library loads plugins from META-INF/services and configuration files from the classpath. In a Spring Boot fat jar and in a Jakarta EE server, some resources are not found or are found twice. Explain how resource lookup differs and how to make the library robust.

ClassLoader.getResources returns every match across a loader and all its parents, while getResource returns only the first, so a META-INF/services file or config file can be found twice when its jar is visible through two loaders, or not at all when we ask a loader that cannot see it. ServiceLoader.load(Service.class) uses the thread context class loader by default, which differs between a fat jar, a Jakarta EE server and a test. A robust library chooses its loader explicitly, merges and de-duplicates results, reads resources through URL streams instead of converting nested jar URLs to files, documents the strategy and tests it in the real packaging.

Why this question is asked

Libraries that discover plugins or read configuration from the class path work perfectly in unit tests and then misbehave in production packaging: a provider is missing in a Jakarta EE server, the same provider is registered twice, or a file read fails only in the Spring Boot jar. These bugs come from assumptions about which class loader sees what. Interviewers ask this scenario to see whether we can reason about loader hierarchies in real deployments and write library code that survives them. In this answer, we will recap how resource lookup works, see why results differ between getResource and getResources and between loaders, look at ServiceLoader and the context class loader, deal with Spring Boot's nested jar URLs, and put together a robust lookup strategy.

1A Quick Recap of Resource Lookup

Resources are found by class loaders with the same search they use for classes. ClassLoader.getResource(name) follows delegation: it asks the parent first and returns the first URL found anywhere in the hierarchy. ClassLoader.getResources(name) returns an enumeration of all matches, first from the parents and then from the loader itself, so if three jars contain META-INF/services/com.acme.Codec, all three URLs come back.

META-INF/services files are how java.util.ServiceLoader discovers implementations: each jar that provides an implementation of com.acme.Codec contains a file of that name listing the implementation class names, one per line. Since many jars can provide implementations, ServiceLoader must read all such files, so it uses getResources.

2Why Resources Go Missing or Appear Twice

Two things decide what a lookup returns: which loader we ask, and which loaders can see the jar. In a Jakarta EE server, a library may be installed in the server's shared library folder, loaded by a server loader, while the application's own jars, including the plugins that implement the library's interface, are loaded by the application's loader, a child of the server loader. If the library looks for providers through its own loader, it asks the parent, which cannot see the child's jars, so the providers are missing.

Duplicates arise in the opposite situation. If the same jar is present both in the server's shared folder and in the application's WEB-INF/lib, then getResources from the application's loader returns the META-INF/services file twice, once from each copy, and the library may register the provider twice, or instantiate two classes with the same name from different loaders. The same happens when a build accidentally places a jar on the class path twice, for example once as a dependency and once inside a shaded jar.

What a lookup sees in each packaging
PackagingLibrary loaded byTypical surprise
IDE or unit testthe application loadereverything visible, all works
Spring Boot fat jarBoot's launcher loadernested jar URLs, not files
EE server, library in WARthe application's loaderworks; app sees itself
EE server, library shareda server loader (parent)own loader misses app providers
Library in both placesparent and child copiesresource found twice

3ServiceLoader and the Thread Context Class Loader

ServiceLoader.load(Codec.class) without a loader argument uses the thread context class loader, a loader attached to each thread, which servers and frameworks set to the current application's loader while they run its code. That is a deliberate choice: it lets a library loaded by a parent loader discover providers in the application, which its own loader cannot see. But it also means the result depends on which thread calls it and what that thread's context loader happens to be.

Java
ServiceLoader.load(Codec.class); // thread context class loader
ServiceLoader.load(Codec.class, Codec.class.getClassLoader()); // the library's own loader
ServiceLoader.load(Codec.class, applicationLoader); // an explicit loader we were given
// a background thread created by the library at start-up may have the server's loader as context:
executor.submit(() -> ServiceLoader.load(Codec.class).stream().count()); // 0 in the EE server

Notice the last line. A thread pool created by the library inherits the context loader of the thread that created it, which may be a server thread, and providers that were visible in a request thread disappear in the background thread. A robust library therefore decides the loader once, at initialisation, while it knows which application it serves, and keeps that loader rather than reading the context loader at arbitrary times.

4Spring Boot's Nested Jar URLs

In a Spring Boot executable jar, dependencies are jars nested inside BOOT-INF/lib of the outer jar. Since Spring Boot 3.2, resources in them have URLs of the form jar:nested:/opt/app/app.jar/!BOOT-INF/lib/acme-codecs.jar!/META-INF/services/com.acme.Codec, handled by a URL handler that Boot registers; older versions used a different format. These URLs are not file paths, and code that converts them does not work.

Java
URL url = loader.getResource("acme/defaults.properties");
new File(url.toURI()); // IllegalArgumentException: URI is not hierarchical (or a similar error)
Paths.get(url.toURI()); // FileSystemNotFoundException for the nested scheme
try (InputStream in = url.openStream()) { // works for file:, jar: and jar:nested: alike
props.load(in);
}

The rule is to treat a resource URL as something to open, never as something to turn into a path. If a library needs to list a directory of resources, it should use an index file listing the names, because directory listing inside nested jars is not reliably supported by plain JDK APIs.

5Making the Library Robust

Putting these points together gives a lookup strategy that behaves the same in every packaging.

Java
public final class CodecRegistry {
public static CodecRegistry create(ClassLoader preferred) {
ClassLoader loader = preferred != null ? preferred
: Thread.currentThread().getContextClassLoader() != null
? Thread.currentThread().getContextClassLoader()
: CodecRegistry.class.getClassLoader();
Map<String, Codec> byName = new LinkedHashMap<>();
for (Codec c : ServiceLoader.load(Codec.class, loader)) {
byName.putIfAbsent(c.getClass().getName(), c); // one entry per implementation name
}
return new CodecRegistry(byName.values(), loader); // keep the loader for later lookups
}
}

The library accepts an explicit loader from its caller, a framework integration such as a Spring configuration can pass the application's loader, and falls back to the context loader and then its own loader. It de-duplicates by implementation class name, so a jar visible twice does not register twice. For configuration files, it uses getResources, decides deliberately whether to merge all matches or take the first, and reads each with openStream. And it documents that strategy, so users who package it in an unusual way know what to expect.

Finally, the library is tested in the real packagings: an integration test that builds a Spring Boot jar and runs it, and one that deploys to the target Jakarta EE server with the library both shared and bundled. Tests from the IDE alone use directory class paths and a single loader, which hide every problem described here.

6Follow-up questions

Should a library ever use getResource instead of getResources?

Yes, when exactly one copy is expected and the first one in delegation order is the right one, such as a default configuration file that an application may override by placing its own copy earlier. But then the library should say so, because first-found depends on loader order. For anything where several jars contribute, such as service files or extension descriptors, getResources and merging is the only correct choice.

Do named modules change service discovery?

Yes. In named modules, providers are declared with provides in module-info and consumers with uses, and ServiceLoader finds providers from the module graph rather than from META-INF/services files. Jars on the class path still use the files. A library that supports both should keep the META-INF/services file and also declare provides, so it works in either mode.

7Conclusion

In this answer, we saw that getResource returns the first match in delegation order while getResources returns every match across the hierarchy, so resources can go missing when we ask a loader that cannot see the jar and appear twice when a jar is visible through two loaders. ServiceLoader uses the thread context class loader by default, which differs by thread and packaging, and Spring Boot's jar:nested: URLs cannot be converted to files. A robust library takes an explicit loader with sensible fallbacks, de-duplicates results, reads resources through streams, documents its strategy, and is tested in the real packaging.

What to say in the interviewAbout 100 seconds aloud

Open with

The differences come from which class loader does the lookup and which loaders can see each jar. getResource returns the first match in delegation order, parents first, while getResources returns all matches across the hierarchy.

Then, if they want more

ServiceLoader reads all META-INF/services files with that name, so if the same jar is visible through two loaders, say in the server's shared library folder and in WEB-INF/lib, the provider is found twice.

In the other direction, if the library sits in a parent loader and looks up through its own loader, it cannot see providers in the application's jars, so they are missing.

ServiceLoader.load with just the interface uses the thread context class loader, which servers set to the application's loader during requests, but a background thread the library created may have a different one, so results can change by thread.

In a Spring Boot fat jar, resources inside nested jars have jar:nested URLs, which are not file paths, so converting them to File or Path fails; you have to open them as streams.

To make the library robust I would let callers pass an explicit class loader, fall back to the context loader and then the library's own loader, decide it once at initialisation, de-duplicate providers by class name, use getResources when several jars can contribute, read everything with openStream, document the strategy, and run integration tests in a real Boot jar and in the target server, not just in the IDE.

About this answer

Last updated
12 November 2026
Versions it names
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 library loads plugins from META-INF/services and configuration files from the...": https://scrivio-interview-library.netlify.app/java/class-loading/0833fb50ef/