{"record":{"id":"280e35a8971ea5ab","repo":"testcontainers/testcontainers-java","slug":"could-not-load-classpath-init-script-initscriptpath-resource-280e35","errorCode":null,"errorMessage":"Could not load classpath init script: ${initScriptPath}. Resource not found.","messagePattern":"Could not load classpath init script: (.+?)\\. Resource not found\\.","errorType":"exception","errorClass":"ScriptLoadException","httpStatus":null,"severity":"error","filePath":"modules/database-commons/src/main/java/org/testcontainers/ext/ScriptUtils.java","lineNumber":207,"sourceCode":"            }\n        }\n        return false;\n    }\n\n    /**\n     * Load script from classpath and apply it to the given database\n     *\n     * @param databaseDelegate database delegate for script execution\n     * @param initScriptPath   the resource to load the init script from\n     */\n    public static void runInitScript(DatabaseDelegate databaseDelegate, String initScriptPath) {\n        try {\n            URL resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath);\n            if (resource == null) {\n                resource = ScriptUtils.class.getClassLoader().getResource(initScriptPath);\n                if (resource == null) {\n                    LOGGER.warn(\"Could not load classpath init script: {}\", initScriptPath);\n                    throw new ScriptLoadException(\n                        \"Could not load classpath init script: \" + initScriptPath + \". Resource not found.\"\n                    );\n                }\n            }\n            String scripts = IOUtils.toString(resource, StandardCharsets.UTF_8);\n            executeDatabaseScript(databaseDelegate, initScriptPath, scripts);\n        } catch (IOException e) {\n            LOGGER.warn(\"Could not load classpath init script: {}\", initScriptPath);\n            throw new ScriptLoadException(\"Could not load classpath init script: \" + initScriptPath, e);\n        } catch (ScriptException e) {\n            LOGGER.error(\"Error while executing init script: {}\", initScriptPath, e);\n            throw new UncategorizedScriptException(\"Error while executing init script: \" + initScriptPath, e);\n        }\n    }\n\n    public static void executeDatabaseScript(DatabaseDelegate databaseDelegate, String scriptPath, String script)\n        throws ScriptException {\n        executeDatabaseScript(","sourceCodeStart":189,"sourceCodeEnd":225,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/modules/database-commons/src/main/java/org/testcontainers/ext/ScriptUtils.java#L189-L225","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ncontainer.withInitScript(\"scripts/init.sql\"); // file is actually at resources/init.sql\n// after\ncontainer.withInitScript(\"init.sql\");","handlingStrategy":"validation","validationCode":"String path = \"init.sql\";\nif (Thread.currentThread().getContextClassLoader().getResource(path) == null\n    && ScriptUtils.class.getClassLoader().getResource(path) == null) {\n    throw new IllegalStateException(\"Init script not on classpath: \" + path);\n}","typeGuard":null,"tryCatchPattern":"try {\n    container.withInitScript(path);\n} catch (ScriptLoadException e) {\n    if (e.getMessage().endsWith(\"Resource not found.\")) {\n        // check the path against src/test/resources\n    }\n    throw e;\n}","preventionTips":["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."],"tags":["classpath","init-script","resource-not-found","sql"],"backgroundTag":"resource-not-found","analyzedSha":"8e549514e3f01c57d70546fbb8599d138f3903e5","analyzedAt":"2026-09-12T14:56:41.227Z","contentChangedAt":"2026-09-12T14:56:41.227Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}