testcontainers/testcontainers-java · error · ScriptLoadException

Could not load classpath init script

Error message

Could not load classpath init script: {}

What it means

ScriptUtils.runInitScript resolves the init script first via the context ClassLoader, then via ScriptUtils' own ClassLoader. If both return null, it logs this warning and throws ScriptLoadException stating the resource was not found. This shared utility backs init-script execution for many RDBMS containers (JDBC, MySQL, PostgreSQL, MSSQL, etc.).

Solutions

  1. Place the script in src/test/resources and pass a path relative to the classpath root
  2. Verify with getClass().getClassLoader().getResource("init/script.sql") in a quick test or debugger breakpoint
  3. Check build config doesn't exclude *.sql from resources (maven-resources filtering, Gradle sourceSets)
  4. Rebuild clean so the resource is present in target/test-classes or build/resources/test

Example fix

// before
container.withInitScript("sql/init.sql"); // file actually at src/test/resources/init.sql
// after
container.withInitScript("init.sql");
Defensive patterns

Strategy: validation

Validate before calling

static void requireClasspathScript(String path) {
    boolean found = Thread.currentThread().getContextClassLoader().getResource(path) != null
        || ScriptUtils.class.getClassLoader().getResource(path) != null;
    if (!found) throw new IllegalStateException("SQL init script not on classpath: " + path);
}

Try / catch

try {
    ScriptUtils.runInitScript(delegate, "db/init.sql");
} catch (ScriptLoadException e) {
    throw new IllegalStateException("Add db/init.sql to src/test/resources (classpath root relative)", e);
}

Prevention

When it happens

Trigger: withInitScript("script.sql") where the SQL file is not on the test classpath under either classloader: wrong path, missing resource directory config, or file never copied into build output.

Common situations: MySQL/PostgreSQL containers in Spring Boot tests where the script is in src/main/resources but tests use only the test classpath; typo like "init/script.sql" vs "init_script.sql"; resources excluded by build filters.

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

Appendix: source

Thrown at modules/database-commons/src/main/java/org/testcontainers/ext/ScriptUtils.java:206

                return true;
            }
        }
        return false;
    }

    /**
     * Load script from classpath and apply it to the given database
     *
     * @param databaseDelegate database delegate for script execution
     * @param initScriptPath   the resource to load the init script from
     */
    public static void runInitScript(DatabaseDelegate databaseDelegate, String initScriptPath) {
        try {
            URL resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath);
            if (resource == null) {
                resource = ScriptUtils.class.getClassLoader().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 scripts = IOUtils.toString(resource, StandardCharsets.UTF_8);
            executeDatabaseScript(databaseDelegate, initScriptPath, scripts);
        } 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 UncategorizedScriptException("Error while executing init script: " + initScriptPath, e);
        }
    }

    public static void executeDatabaseScript(DatabaseDelegate databaseDelegate, String scriptPath, String script)
        throws ScriptException {

View on GitHub (pinned to 8e549514e3)