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

Could not load classpath init script

Error message

Could not load classpath init script: ${initScriptPath}

What it means

After locating the init script resource, CassandraContainer reads its CQL content with IOUtils.toString. If reading the stream throws an IOException (unreadable/broken resource), the container throws ScriptLoadException wrapping it, logging 'Could not load classpath init script: <path>'.

Solutions

  1. Rebuild the project and inspect the packaged resource in target/test-classes or the jar
  2. Disable aggressive Maven resource filtering for .cql files (nonFilteredFileExtensions) so the file isn't corrupted
  3. Ensure the file is plain UTF-8 text and readable by the test process
Defensive patterns

Strategy: validation

Validate before calling

try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(initScript)) {
  if (in == null || in.read() == -1) throw new IllegalStateException("init script empty or unreadable");
}

Try / catch

try { container.start(); } catch (ContainerLaunchException e) { /* inspect ScriptLoadException cause (IOException) */ }

Prevention

When it happens

Trigger: runInitScriptIfRequired during container.start() when the classpath resource for initScriptPath was found but its content could not be read (IOException from IOUtils.toString).

Common situations: Corrupted or unreadable resource in a jar; resource filtered/binary-corrupted by build plugins; charset/IO issues in packaged artifacts.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


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

Appendix: source

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

    /**
     * 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
                );
            }
        }
    }

    /**
     * Map (effectively replace) directory in Docker with the content of resourceLocation if resource location is not null
     *
     * Protected to allow for changing implementation by extending the class
     *
     * @param pathNameInContainer path in docker
     * @param resourceLocation    relative classpath to resource
     */

View on GitHub (pinned to 8e549514e3)