{"record":{"id":"a440f2068cc230a2","repo":"testcontainers/testcontainers-java","slug":"could-not-load-classpath-init-script","errorCode":null,"errorMessage":"Could not load classpath init script: {}","messagePattern":"Could not load classpath init script: (.+?)","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java","lineNumber":228,"sourceCode":"                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).\n     *\n     * @param connectionUrl {@link ConnectionUrl} instance representing JDBC Url with r init function declarations.\n     * @param connection    JDBC connection to apply init functions to.\n     * @throws SQLException on script or DB error\n     */\n    private void runInitFunctionIfRequired(final ConnectionUrl connectionUrl, Connection connection)\n        throws SQLException {\n        if (connectionUrl.getInitFunction().isPresent()) {\n            String className = connectionUrl.getInitFunction().get().getClassName();","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java#L210-L246","documentation":"If reading the init script resource throws an IOException, runInitScriptIfRequired wraps it in this SQLException. The resource was found but its content could not be read (I/O error while loading it).","triggerScenarios":"TC_INITSCRIPT resource resolves but IOUtils.toString fails reading it — corrupted resource, unreadable file, closed stream, or an IOException while reading from the resolved URL.","commonSituations":"File permission issues on CI; network-mounted resources failing mid-read; unusual classloader (fat jar / shaded) that can open but not stream the resource.","solutions":["Check file permissions and that the resource is readable by the test process user","Re-copy/rebuild the resource in the build output; verify integrity","If loading from a jar, ensure the jar is complete and not corrupted","As an alternative, pass the script inline with TC_INITSQL or use withInitScript on the container"],"exampleFix":"// before\nString url = \"jdbc:tc:postgresql:16:///mydb?TC_INITSCRIPT=file:/mnt/share/init.sql\";\n// after\nString url = \"jdbc:tc:postgresql:16:///mydb?TC_INITSCRIPT=classpath:sql/init.sql\"; // local, packaged resource","handlingStrategy":"try-catch","validationCode":"URL r = getClass().getClassLoader().getResource(initPath);\nif (r == null) throw new IllegalStateException(\"Missing resource: \" + initPath);\ntry (InputStream in = r.openStream()) { in.readAllBytes(); } // readable check","typeGuard":null,"tryCatchPattern":"try {\n    runTestWithContainer();\n} catch (SQLException e) {\n    if (e.getCause() instanceof IOException) {\n        throw new IllegalStateException(\"Init script unreadable: \" + e.getMessage(), e);\n    }\n    throw e;\n}","preventionTips":["Prefer packaged classpath resources over external file URLs","Check file permissions in CI containers","Avoid shaded-jar setups that break resource streaming"],"tags":["jdbc","testcontainers","init-script","io"],"backgroundTag":"file-read-failed","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"}