testcontainers/testcontainers-java · error · ScriptLoadException
Could not load classpath init script
Error message
Could not load classpath init script: {} What it means
In the newer CassandraContainer (modules/cassandra), runInitScriptIfRequired copies the classpath init script into the container and executes it. When the classpath resource cannot be resolved (MountableFile/IllegalArgumentException), it logs this warning and throws ScriptLoadException. The configured init script simply was not found on the classpath.
Solutions
- Place the CQL script under src/test/resources and reference it relative to the classpath root, e.g. withInitScript("cql/init.cql") for src/test/resources/cql/init.cql
- Verify the resource exists at runtime: getClass().getClassLoader().getResourceAsStream("cql/init.cql") != null
- Rebuild so resources are copied (mvn test vs mvn package inconsistencies; Gradle processResources)
- Catch ScriptLoadException in test setup and print the resolved classpath for debugging
Example fix
// before
new CassandraContainer<>("cassandra:4.0").withInitScript("src/test/resources/init.cql");
// after
new CassandraContainer<>("cassandra:4.0").withInitScript("init.cql"); // file at src/test/resources/init.cql Defensive patterns
Strategy: validation
Validate before calling
static void assertClasspathResource(String path) {
if (Thread.currentThread().getContextClassLoader().getResource(path) == null) {
throw new IllegalStateException("Init script not on classpath: " + path);
}
} Try / catch
try {
container.start();
} catch (ScriptLoadException e) {
throw new IllegalStateException("Fix withInitScript path (relative to classpath root): " + e.getMessage(), e);
} Prevention
- Reference init scripts relative to the classpath root, never with src/test/resources prefixes
- Keep script filenames case-exact (CI Linux is case-sensitive)
- Add the script under src/test/resources and verify it appears in target/test-classes after build
- Smoke-test resource lookup with getClass().getClassLoader().getResource() in a unit test
When it happens
Trigger: Calling .withInitScript("init.cql") where the resource cannot be loaded from the classpath: wrong path, file outside the classpath root, missing leading consideration (paths are relative to classpath root), or resource packaged in a dependency that isn't on the runtime classpath.
Common situations: Typo in the script path; script lives in src/test/resources but the path includes src/test/resources prefix; file placed under a package directory but referenced without the package path; Maven/Gradle not copying resources.
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
- Could not load classpath init script
- Could not load classpath init script
- Could not load classpath init script
- Could not load classpath init script
- Could not load classpath init script
AI-assisted analysis of testcontainers/testcontainers-java@8e549514e3 (2026-09-12).
Data as JSON: /api/errors/d18297eb3de54e76.
Report an issue: GitHub.
Appendix: source
Thrown at modules/cassandra/src/main/java/org/testcontainers/cassandra/CassandraContainer.java:104
protected void containerIsStarted(InspectContainerResponse containerInfo) {
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 ifView on GitHub (pinned to 8e549514e3)