Skip to content

Class and resource lookups in executable jars open a URL connection for every nested jar #52245

Closed as duplicate

Description

@janwaleed09

When an application runs with java -jar, JarUrlClassLoader leaves class and resource lookups to URLClassLoader. Spring Boot's jar: URLs don't use the JDK's default handler, so URLClassPath uses its generic Loader for them instead of JarLoader (the isDefaultJarHandler check, see JDK-8174151). The generic loader creates a new URL and opens a URLConnection for every nested jar until it finds the entry, and it reports a miss through an exception. definePackageIfNecessary then scans the jars again for each new package, because the generic loader doesn't provide the manifest.

I measured this with Spring PetClinic (Boot 4.1.0, 92 nested jars) and a sample app with web, JPA, security and actuator (4.2.0-SNAPSHOT, 101 nested jars):

  • One startup of the sample app opened 577,695 connections, and 97.9% of them were misses. On average, a class was found after about 45 jars.
  • At runtime, each /owners/{id} page view in PetClinic opens 1,128 connections. Thymeleaf links go through ResourceUrlEncodingFilter, which looks up every link in the 4 static locations. Links like /owners/find are never found, and the misses are not cached. A 404 opens 376 connections.

I tried a change in JarUrlClassLoader that looks up entries with NestedJarFile directly and only creates a URL when the entry exists. Packages are defined with the manifest of the jar that contains the class. It is not a cache, it doesn't change the URL format, and it falls back to the current code for other URLs.

Results with Docker --cpus=2 and Temurin 25.0.4.1, both apps running with the loader from current main. The machine was busy, so these are medians of 8 to 16 interleaved runs:

PetClinic sample app
startup time -8% -13%
startup time with AOT cache -14% -18%
memory allocated during startup 772 MB -> 437 MB 698 MB -> 349 MB
CPU per /owners/{id} request 3.11 ms -> 2.55 ms
CPU per 404 0.70 ms -> 0.51 ms

With the 4.1.0 loader, the startup difference was about 20%, because #51827 already removed part of the cost.

The AOT cache helps the executable jar too. On JDK 25, 85% of PetClinic's classes came from the cache, although the docs say that it has no effect without extracting. But it can't remove the lookups, because the class loader still has to find and read every class before the JVM can use the cached one. The extracted form is still clearly faster (with the AOT cache, PetClinic starts in 1.96 s extracted versus 3.43 s with the change), so this doesn't change the recommendation to extract.

For both apps, I compared class code sources, getResource/getResources results and multi-release entries with the current loader (for PetClinic: 15,268 classes and 1,397 resource lookups). There were no differences. Package attributes now match the JDK's application class loader for all classes. This also fixes a case that #52244 doesn't cover: log4j-to-slf4j looks up the missing class org.slf4j.helpers.Log4jLoggerFactory, which adds org.slf4j.helpers to undefinablePackages, so that package never gets its attributes.

I'll open a pull request with the change and link it here.

Activity

  1. added
    status: waiting-for-triage
    on Oct 15, 2026
  2. wilkinsona commented on Oct 15, 2026

    @wilkinsona
    Member

    Thanks, @janwaleed09. If you intend to immediately open a PR there's no need for an issue as well.

  3. added
    status: duplicate
    and removed
    status: waiting-for-triage
    on Oct 15, 2026
  4. janwaleed09 commented on Oct 15, 2026

    @janwaleed09
    ContributorAuthor

    Thanks, that makes sense I only used the issue to keep the measurements in one place, and spring-projects/spring-boot#52246 links to it, so I’ll put everything in the PR next time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: duplicate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions