testcontainers/testcontainers-java · error · java.lang.IllegalArgumentException

Resource with path could not be found on any of these…

Error message

Resource with path ${resourcePath} could not be found on any of these classloaders: ${classLoadersToSearch}

What it means

MountableFile.getClasspathResource resolves a classpath resource path using several classloaders. If no classloader can find the resource, it throws IllegalArgumentException listing the classloaders searched. This means the path given to MountableFile.forClasspathResource(...) does not point to an existing resource on the runtime classpath.

Solutions

  1. Verify the resource exists on the runtime classpath: move it under src/test/resources (or src/main/resources) with the exact path used in the call.
  2. Use the classpath-relative path (no leading slash ambiguity — try both) and check exact casing.
  3. Confirm the build isn't excluding/filtering the resource and that the file is packaged in the jar you run tests from.
  4. If you actually meant a filesystem file, use MountableFile.forHostPath instead.

Example fix

// before
MountableFile.forClasspathResource("config/init.sql");
// after: place file at src/test/resources/config/init.sql, then
MountableFile.forClasspathResource("config/init.sql"); // or "/config/init.sql"
Defensive patterns

Strategy: validation

Validate before calling

String path = "config/init.sql";
if (getClass().getClassLoader().getResource(path) == null) {
    throw new IllegalStateException("Classpath resource missing before mount: " + path);
}

Try / catch

try {
    MountableFile f = MountableFile.forClasspathResource("config/init.sql");
} catch (IllegalArgumentException e) {
    if (e.getMessage().startsWith("Resource with path")) {
        log.error("Resource not on classpath: {}", e.getMessage());
    }
    throw e;
}

Prevention

When it happens

Trigger: MountableFile.forClasspathResource("some/file.txt") (or forClasspathResource(path, offset)) where no file exists at that classpath-relative path — wrong path, missing leading handling, file not under src/main/resources or src/test/resources, or excluded from the built jar.

Common situations: Specifying an absolute filesystem path instead of a classpath path; file exists in src/test/java (not copied as a resource); resource filtered out by build config; running from an environment where the resource is in a different module's classpath; case mismatch in the resource name.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of testcontainers/testcontainers-java@8e549514e3 (2026-09-12). Data as JSON: /api/errors/374d477a0bd88908. Report an issue: GitHub.

Appendix: source

Thrown at core/src/main/java/org/testcontainers/utility/MountableFile.java:158

            if (classLoader == null) {
                continue;
            }

            URL resource = classLoader.getResource(resourcePath);
            if (resource != null) {
                return resource;
            }

            // Be lenient if an absolute path was given
            if (resourcePath.startsWith("/")) {
                resource = classLoader.getResource(resourcePath.replaceFirst("/", ""));
                if (resource != null) {
                    return resource;
                }
            }
        }

        throw new IllegalArgumentException(
            "Resource with path " +
            resourcePath +
            " could not be found on any of these classloaders: " +
            classLoadersToSearch
        );
    }

    private static String unencodeResourceURIToFilePath(@NotNull final String resource) {
        try {
            // Convert any url-encoded characters (e.g. spaces) back into unencoded form
            return URLDecoder
                .decode(resource.replaceAll("\\+", "%2B"), Charsets.UTF_8.name())
                .replaceFirst("jar:", "")
                .replaceFirst("file:", "")
                .replaceAll("!.*", "");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException(e);
        }

View on GitHub (pinned to 8e549514e3)