testcontainers/testcontainers-java · error · ScriptLoadException
Could not load classpath init script
Error message
Could not load classpath init script: ${initScriptPath}. Resource not found. What it means
ScriptUtils.runInitScript could not resolve initScriptPath on either the context classloader or ScriptUtils' own classloader, so it logs a warning and throws ScriptLoadException with this message. The init script resource you asked for does not exist on the classpath.
Solutions
- Verify the file exists at src/test/resources/<initScriptPath> and the path string matches exactly (case-sensitive).
- Print Thread.currentThread().getContextClassLoader().getResource(path) in a debug snippet to confirm resolution.
- Check build configuration (resource filters, maven/gradle excludes) isn't stripping the file from the classpath.
- Use a classpath-relative path like 'init/schema.sql', not a filesystem path.
Example fix
// before
container.withInitScript("scripts/init.sql"); // file is actually at resources/init.sql
// after
container.withInitScript("init.sql"); Defensive patterns
Strategy: validation
Validate before calling
String path = "init.sql";
if (Thread.currentThread().getContextClassLoader().getResource(path) == null
&& ScriptUtils.class.getClassLoader().getResource(path) == null) {
throw new IllegalStateException("Init script not on classpath: " + path);
} Try / catch
try {
container.withInitScript(path);
} catch (ScriptLoadException e) {
if (e.getMessage().endsWith("Resource not found.")) {
// check the path against src/test/resources
}
throw e;
} Prevention
- Keep init scripts under src/test/resources and reference them with classpath-relative paths.
- Add a unit assertion that the resource resolves before spinning up containers.
- Watch build resource excludes/filters.
When it happens
Trigger: withInitScript("foo.sql") where no file named foo.sql exists under src/test/resources (or the resolved classpath), or a typo in the path such as missing directories.
Common situations: Script placed in src/main/resources instead of test resources; path missing leading directory; file excluded by build filters; using an absolute path instead of a classpath-relative one.
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/280e35a8971ea5ab.
Report an issue: GitHub.
Appendix: source
Thrown at modules/database-commons/src/main/java/org/testcontainers/ext/ScriptUtils.java:207
}
}
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 {
executeDatabaseScript(View on GitHub (pinned to 8e549514e3)