{"record":{"id":"8163a6ae3df98d6d","repo":"testcontainers/testcontainers-java","slug":"could-not-load-classpath-init-script-scriptutils","errorCode":null,"errorMessage":"Could not load classpath init script: {}","messagePattern":"Could not load classpath init script: (.+?)","errorType":"console","errorClass":"ScriptLoadException","httpStatus":null,"severity":"error","filePath":"modules/database-commons/src/main/java/org/testcontainers/ext/ScriptUtils.java","lineNumber":206,"sourceCode":"                return true;\n            }\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 {","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/modules/database-commons/src/main/java/org/testcontainers/ext/ScriptUtils.java#L188-L224","documentation":"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.).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Place the script in src/test/resources and pass a path relative to the classpath root","Verify with getClass().getClassLoader().getResource(\"init/script.sql\") in a quick test or debugger breakpoint","Check build config doesn't exclude *.sql from resources (maven-resources filtering, Gradle sourceSets)","Rebuild clean so the resource is present in target/test-classes or build/resources/test"],"exampleFix":"// before\ncontainer.withInitScript(\"sql/init.sql\"); // file actually at src/test/resources/init.sql\n// after\ncontainer.withInitScript(\"init.sql\");","handlingStrategy":"validation","validationCode":"static void requireClasspathScript(String path) {\n    boolean found = Thread.currentThread().getContextClassLoader().getResource(path) != null\n        || ScriptUtils.class.getClassLoader().getResource(path) != null;\n    if (!found) throw new IllegalStateException(\"SQL init script not on classpath: \" + path);\n}","typeGuard":null,"tryCatchPattern":"try {\n    ScriptUtils.runInitScript(delegate, \"db/init.sql\");\n} catch (ScriptLoadException e) {\n    throw new IllegalStateException(\"Add db/init.sql to src/test/resources (classpath root relative)\", e);\n}","preventionTips":["Store SQL init scripts in src/test/resources and reference them root-relative","Check build resource excludes/filters don't drop .sql files","Verify target/test-classes contains the script before running tests","Use the same path string in withInitScript and on-disk layout"],"tags":["sql","init-script","classpath","resource-not-found"],"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"}