testcontainers/testcontainers-java · error · org.testcontainers.ext.ScriptUtils.ScriptLoadException

Could not load classpath init script

Error message

Could not load classpath init script: ${initScriptPath}. Resource not found.

What it means

CassandraContainer.runInitScriptIfRequired applies the classpath init script (set via withInitScript) after startup. MountableFile.forClasspathResource throws IllegalArgumentException when the resource cannot be found; the container wraps it in a ScriptLoadException with this message. The named classpath resource does not exist.

Solutions

  1. Verify the resource exists on the test classpath at exactly the given path (getResource("/cql/init.cql") != null)
  2. Fix the path to be classpath-root relative and check case sensitivity (e.g. "init.cql" if at resources root)
  3. Ensure build config copies the resource (check target/test-classes or build/resources for the file)
  4. If the script lives on the filesystem instead, load it via a File path and use the appropriate copy mechanism rather than a classpath resource

Example fix

// before
new CassandraContainer<>("cassandra:5").withInitScript("schema/init.cql"); // file actually at resources/schema.cql
// after
new CassandraContainer<>("cassandra:5").withInitScript("schema.cql"); // matches classpath root location
Defensive patterns

Strategy: validation

Validate before calling

String script = "schema.cql";
if (getClass().getClassLoader().getResource(script) == null)
    throw new IllegalStateException("Init script not on classpath: " + script);
cassandra.withInitScript(script);

Type guard

boolean classpathResourceExists(String path) {
    return Thread.currentThread().getContextClassLoader().getResource(
        path.startsWith("/") ? path.substring(1) : path) != null;
}

Try / catch

try {
    container.start();
} catch (ScriptLoadException e) {
    if (e.getMessage().contains("Resource not found")) {
        throw new IllegalStateException("Check withInitScript path: " + container.getInitScript(), e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling withInitScript("cql/init.cql") (or setting cassandra.container.init.script) where no file at that classpath path exists; wrong package-relative path (leading slash semantics), file excluded from the built jar/test resources, or typo in the path.

Common situations: Script placed in src/main/resources but tests run with a different classpath; Maven/Gradle resource filtering or excludes dropping .cql files; path missing the leading directories relative to classpath root; refactoring moved resources.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


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

Appendix: source

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

        runInitScriptIfRequired();
    }

    /**
     * Load init script content and apply it to the database if initScriptPath is set
     */
    private void runInitScriptIfRequired() {
        if (this.initScriptPath != null) {
            try {
                final MountableFile originalInitScript = MountableFile.forClasspathResource(this.initScriptPath);
                // The init script is executed as is by the cqlsh command, so copy it into the container. The name
                // of the script is generic since it's not important to keep the original name.
                copyFileToContainer(originalInitScript, DEFAULT_INIT_SCRIPT_FILENAME);
                new CassandraDatabaseDelegate(this).execute(null, DEFAULT_INIT_SCRIPT_FILENAME, -1, false, false);
            } catch (IllegalArgumentException e) {
                // MountableFile.forClasspathResource will throw an IllegalArgumentException if the resource cannot
                // be found.
                logger().warn("Could not load classpath init script: {}", this.initScriptPath);
                throw new ScriptLoadException(
                    "Could not load classpath init script: " + this.initScriptPath + ". Resource not found.",
                    e
                );
            } catch (ScriptUtils.ScriptStatementFailedException e) {
                logger().error("Error while executing init script: {}", this.initScriptPath, e);
                throw new ScriptUtils.UncategorizedScriptException(
                    "Error while executing init script: " + this.initScriptPath,
                    e
                );
            }
        }
    }

    /**
     * Initialize Cassandra with the custom overridden Cassandra configuration
     * <p>
     * Be aware, that Docker effectively replaces all /etc/cassandra content with the content of config location, so if
     * Cassandra.yaml in configLocation is absent or corrupted, then Cassandra just won't launch.

View on GitHub (pinned to 8e549514e3)