{"record":{"id":"07f6a111ea99d25e","repo":"testcontainers/testcontainers-java","slug":"could-not-load-classpath-init-script-resource-not-found","errorCode":null,"errorMessage":"Could not load classpath init script: {}. Resource not found.","messagePattern":"Could not load classpath init script: (.+?)\\. Resource not found\\.","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java","lineNumber":219,"sourceCode":"     * @param databaseDelegate database delegate to apply init scripts to the database\n     * @throws SQLException on script or DB error\n     */\n    private void runInitScriptIfRequired(final ConnectionUrl connectionUrl, DatabaseDelegate databaseDelegate)\n        throws SQLException {\n        if (connectionUrl.getInitScriptPath().isPresent()) {\n            String initScriptPath = connectionUrl.getInitScriptPath().get();\n            try {\n                URL resource;\n                if (initScriptPath.startsWith(FILE_PATH_PREFIX)) {\n                    //relative workdir path\n                    resource = new URL(initScriptPath);\n                } else {\n                    //classpath resource\n                    resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath);\n                }\n                if (resource == null) {\n                    LOGGER.warn(\"Could not load classpath init script: {}\", initScriptPath);\n                    throw new SQLException(\n                        \"Could not load classpath init script: \" + initScriptPath + \". Resource not found.\"\n                    );\n                }\n\n                String sql = IOUtils.toString(resource, StandardCharsets.UTF_8);\n                ScriptUtils.executeDatabaseScript(databaseDelegate, initScriptPath, sql);\n            } catch (IOException e) {\n                LOGGER.warn(\"Could not load classpath init script: {}\", initScriptPath);\n                throw new SQLException(\"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 SQLException(\"Error while executing init script: \" + initScriptPath, e);\n            }\n        }\n    }\n\n    /**\n     * Run an init function (must be a public static method on an accessible class).","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java#L201-L237","documentation":"runInitScriptIfRequired throws this SQLException when the init script configured via TC_INITSCRIPT points to a classpath resource that cannot be found. Testcontainers logs a warning and fails the connection because the requested initialization SQL could not be located.","triggerScenarios":"jdbc:tc URL contains TC_INITSCRIPT=classpath:some/path/init.sql (or a plain path) but ClassLoader.getResource returns null — file not in src/test/resources, wrong path casing, or file not copied to the build output.","commonSituations":"init.sql placed in src/main/resources instead of src/test/resources; missing leading slash semantics confusion; build tool not copying resources; typo in the TC_INITSCRIPT path.","solutions":["Verify the file exists at the exact classpath path and is under src/test/resources (or src/main/resources)","Prefix the path with classpath: in TC_INITSCRIPT, e.g. TC_INITSCRIPT=classpath:sql/init.sql","Rebuild / run resource copying so the file lands in target/test-classes or build/resources/test","Check path casing and extension — classpath lookups are case-sensitive on Linux CI"],"exampleFix":"// before\nString url = \"jdbc:tc:postgresql:16:///mydb?TC_INITSCRIPT=init.sql\";\n// after\nString url = \"jdbc:tc:postgresql:16:///mydb?TC_INITSCRIPT=classpath:sql/init.sql\"; // file at src/test/resources/sql/init.sql","handlingStrategy":"validation","validationCode":"String path = \"sql/init.sql\";\nif (getClass().getClassLoader().getResource(path) == null)\n    throw new IllegalStateException(\"Init script not on classpath: \" + path);","typeGuard":null,"tryCatchPattern":"try {\n    runTestWithContainer();\n} catch (SQLException e) {\n    if (e.getMessage().contains(\"Resource not found\")) {\n        throw new IllegalStateException(\"Check TC_INITSCRIPT path and resources dir\", e);\n    }\n    throw e;\n}","preventionTips":["Store init scripts under src/test/resources and reference with the classpath: prefix","Rebuild before running tests after moving resource files","Verify exact casing and extension in CI (Linux is case-sensitive)"],"tags":["jdbc","testcontainers","init-script","classpath"],"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"}