testcontainers/testcontainers-java · error

Could not load classpath init script

Error message

Could not load classpath init script: {}

What it means

OrientDBContainer.loadScript logs this warning when the classpath resource for the init script cannot be found, then throws a RuntimeException. Init scripts are loaded from the application classpath via ClassLoader.getResource; a null resource means the path is wrong or the file is not on the classpath. getSession() calls loadScript when withInitScript was configured, so container startup fails.

Solutions

  1. Verify the script exists under src/test/resources and the path matches exactly (case-sensitive).
  2. Check the built jar/classes directory to confirm the resource is copied to the classpath.
  3. Wrap getSession in try-catch for RuntimeException if the script is optional, or remove the withInitScript call.

Example fix

// before
container.withInitScript("init/orient-init.sql"); // file missing on classpath
// after
container.withInitScript("scripts/orient-init.sql"); // file at src/test/resources/scripts/orient-init.sql
Defensive patterns

Strategy: validation

Validate before calling

String path = "scripts/orient-init.sql";
if (getClass().getClassLoader().getResource(path) == null) {
    throw new IllegalStateException("Init script not on classpath: " + path);
}
container.withInitScript(path);

Type guard

boolean classpathResourceExists(String path) {
    return getClass().getClassLoader().getResource(path) != null;
}

Try / catch

try {
    Object session = container.getSession();
} catch (RuntimeException e) {
    if (e.getMessage().contains("Could not load classpath init script")) {
        logger.warn("Init script missing; continuing without it", e);
    } else {
        throw e;
    }
}

Prevention

When it happens

Trigger: Calling withInitScript("scripts/init.sql") where no such resource exists on the classpath (missing from src/test/resources, wrong path, or absent file extension).

Common situations: Script placed in src/main instead of test resources; path given with leading '/' or wrong case on Linux CI; resource excluded by build filters or not copied by Gradle/Maven.

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/171f9dcf006b80c4. Report an issue: GitHub.

Appendix: source

Thrown at modules/orientdb/src/main/java/org/testcontainers/containers/OrientDBContainer.java:177

            }
        } else {
            orientDB.createIfNotExists(databaseName, ODatabaseType.PLOCAL);
        }
        if (session == null) {
            session = orientDB.open(databaseName, username, password);

            scriptPath.ifPresent(path -> loadScript(path, session));
        }
        return session;
    }

    @Deprecated
    private void loadScript(String path, ODatabaseSession session) {
        try {
            URL resource = getClass().getClassLoader().getResource(path);

            if (resource == null) {
                LOGGER.warn("Could not load classpath init script: {}", scriptPath);
                throw new RuntimeException(
                    "Could not load classpath init script: " + scriptPath + ". Resource not found."
                );
            }

            String script = IOUtils.toString(resource, StandardCharsets.UTF_8);

            session.execute("sql", script);
        } catch (IOException e) {
            LOGGER.warn("Could not load classpath init script: {}", scriptPath);
            throw new RuntimeException("Could not load classpath init script: " + scriptPath, e);
        } catch (UnsupportedOperationException e) {
            LOGGER.error("Error while executing init script: {}", scriptPath, e);
            throw new RuntimeException("Error while executing init script: " + scriptPath, e);
        }
    }
}

View on GitHub (pinned to 8e549514e3)