testcontainers/testcontainers-java · error · ScriptLoadException

Could not load classpath init script

Error message

Could not load classpath init script: {}

What it means

In the legacy CassandraContainer (modules/cassandra → org.testcontainers.containers), runInitScriptIfRequired resolves the init script from the context classloader. If getResource returns null, it logs this warning and throws ScriptLoadException indicating the classpath resource was not found. This is a fail-fast for a misconfigured initScriptPath.

Solutions

  1. Confirm the file exists under src/test/resources and matches initScriptPath exactly (case included)
  2. Reload/refresh build output (mvn clean test or gradle clean test) so the resource lands in target/test-classes
  3. Check Thread.currentThread().getContextClassLoader().getResource(path) directly in a debug test
  4. Use an absolute-classpath-style path relative to the root, e.g. "scripts/init.cql"

Example fix

// before
withInitScript("InitScript.cql"); // actual file: initscript.cql on Linux CI
// after
withInitScript("init.cql"); // exact case-matched name under src/test/resources
Defensive patterns

Strategy: validation

Validate before calling

if (Thread.currentThread().getContextClassLoader().getResource(initScriptPath) == null) {
    throw new IllegalArgumentException("Cassandra init script not found on classpath: " + initScriptPath);
}

Try / catch

try { container.start(); } catch (ScriptLoadException e) { throw new AssertionError("Check withInitScript path and case: " + initScriptPath, e); }

Prevention

When it happens

Trigger: withInitScript(path) was called with a path that the context ClassLoader cannot resolve: file missing from target/test-classes, wrong relative path, or path mistakenly including directory prefixes that don't exist on the classpath.

Common situations: Script in src/main/resources vs src/test/resources mismatch; filename case-sensitivity issues on Linux CI; resources filtered out by build config; running tests from an IDE with stale output directories.

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

Appendix: source

Thrown at modules/cassandra/src/main/java/org/testcontainers/containers/CassandraContainer.java:101

    @Override
    protected void configure() {
        optionallyMapResourceParameterAsVolume(CONTAINER_CONFIG_LOCATION, configLocation);
    }

    @Override
    protected void containerIsStarted(InspectContainerResponse containerInfo) {
        runInitScriptIfRequired();
    }

    /**
     * Load init script content and apply it to the database if initScriptPath is set
     */
    private void runInitScriptIfRequired() {
        if (initScriptPath != null) {
            try {
                URL resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath);
                if (resource == null) {
                    logger().warn("Could not load classpath init script: {}", initScriptPath);
                    throw new ScriptLoadException(
                        "Could not load classpath init script: " + initScriptPath + ". Resource not found."
                    );
                }
                String cql = IOUtils.toString(resource, StandardCharsets.UTF_8);
                DatabaseDelegate databaseDelegate = getDatabaseDelegate();
                ScriptUtils.executeDatabaseScript(databaseDelegate, initScriptPath, cql);
            } catch (IOException e) {
                logger().warn("Could not load classpath init script: {}", initScriptPath);
                throw new ScriptLoadException("Could not load classpath init script: " + initScriptPath, e);
            } catch (ScriptException e) {
                logger().error("Error while executing init script: {}", initScriptPath, e);
                throw new ScriptUtils.UncategorizedScriptException(
                    "Error while executing init script: " + initScriptPath,
                    e
                );
            }
        }

View on GitHub (pinned to 8e549514e3)