testcontainers/testcontainers-java · error · RuntimeException
Could not load classpath init script
Error message
Could not load classpath init script: ${scriptPath} What it means
After resolving the init script resource, loadScript reads it with IOUtils.toString; if that raises IOException (e.g. unreadable/failed stream), it is wrapped in a RuntimeException 'Could not load classpath init script: <path>' with the IOException as cause.
Solutions
- Inspect the wrapped cause (e.getCause()) for the real IO error and fix the underlying resource access
- Ensure the resource is a plain readable file/jar entry on the classpath
- Rebuild/repackage so the resource is fresh and accessible
Defensive patterns
Strategy: try-catch
Try / catch
try {
container.start();
} catch (RuntimeException e) {
if (e.getCause() instanceof IOException) {
log.error("Failed reading init script {}: {}", scriptPath, e.getCause().getMessage());
}
throw e;
} Prevention
- Rebuild jars/resources when scripts change
- Avoid unusual classloader setups for test resources
- Keep init scripts small, plain UTF-8 files
When it happens
Trigger: The classpath resource exists but cannot be read into a String — e.g. resource backed by a broken/unreadable source, custom classloader quirks, or I/O errors while opening the URL stream.
Common situations: Custom classloader/resource setups in build tools or application servers; resource inside a corrupted jar; very large or special-character scripts causing IO trouble.
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
- Error while executing init script
- Could not load classpath init script
- Could not load classpath init script
- Error while executing init script
- Script statement failed at line lineNumber in script…
AI-assisted analysis of testcontainers/testcontainers-java@8e549514e3 (2026-09-12).
Data as JSON: /api/errors/b7b81e0096de83b3.
Report an issue: GitHub.
Appendix: source
Thrown at modules/orientdb/src/main/java/org/testcontainers/containers/OrientDBContainer.java:188
@Deprecated
private void loadScript(String path, ODatabaseSession session) {
try {
URL resource = getClass().getClassLoader().getResource(path);
if (resource == null) {
LOGGER.warn("Could not load classpath init script: {}", scriptPath);
throw new RuntimeException(
"Could not load classpath init script: " + scriptPath + ". Resource not found."
);
}
String script = IOUtils.toString(resource, StandardCharsets.UTF_8);
session.execute("sql", script);
} catch (IOException e) {
LOGGER.warn("Could not load classpath init script: {}", scriptPath);
throw new RuntimeException("Could not load classpath init script: " + scriptPath, e);
} catch (UnsupportedOperationException e) {
LOGGER.error("Error while executing init script: {}", scriptPath, e);
throw new RuntimeException("Error while executing init script: " + scriptPath, e);
}
}
}
View on GitHub (pinned to 8e549514e3)