{"record":{"id":"bae3c57bff9331fb","repo":"testcontainers/testcontainers-java","slug":"could-not-load-classpath-init-script-initscriptpath-resource","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":"org.testcontainers.ext.ScriptUtils.ScriptLoadException","httpStatus":null,"severity":"error","filePath":"modules/cassandra/src/main/java/org/testcontainers/cassandra/CassandraContainer.java","lineNumber":105,"sourceCode":"        runInitScriptIfRequired();\n    }\n\n    /**\n     * Load init script content and apply it to the database if initScriptPath is set\n     */\n    private void runInitScriptIfRequired() {\n        if (this.initScriptPath != null) {\n            try {\n                final MountableFile originalInitScript = MountableFile.forClasspathResource(this.initScriptPath);\n                // The init script is executed as is by the cqlsh command, so copy it into the container. The name\n                // of the script is generic since it's not important to keep the original name.\n                copyFileToContainer(originalInitScript, DEFAULT_INIT_SCRIPT_FILENAME);\n                new CassandraDatabaseDelegate(this).execute(null, DEFAULT_INIT_SCRIPT_FILENAME, -1, false, false);\n            } catch (IllegalArgumentException e) {\n                // MountableFile.forClasspathResource will throw an IllegalArgumentException if the resource cannot\n                // be found.\n                logger().warn(\"Could not load classpath init script: {}\", this.initScriptPath);\n                throw new ScriptLoadException(\n                    \"Could not load classpath init script: \" + this.initScriptPath + \". Resource not found.\",\n                    e\n                );\n            } catch (ScriptUtils.ScriptStatementFailedException e) {\n                logger().error(\"Error while executing init script: {}\", this.initScriptPath, e);\n                throw new ScriptUtils.UncategorizedScriptException(\n                    \"Error while executing init script: \" + this.initScriptPath,\n                    e\n                );\n            }\n        }\n    }\n\n    /**\n     * Initialize Cassandra with the custom overridden Cassandra configuration\n     * <p>\n     * Be aware, that Docker effectively replaces all /etc/cassandra content with the content of config location, so if\n     * Cassandra.yaml in configLocation is absent or corrupted, then Cassandra just won't launch.","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/modules/cassandra/src/main/java/org/testcontainers/cassandra/CassandraContainer.java#L87-L123","documentation":"CassandraContainer.runInitScriptIfRequired applies the classpath init script (set via withInitScript) after startup. MountableFile.forClasspathResource throws IllegalArgumentException when the resource cannot be found; the container wraps it in a ScriptLoadException with this message. The named classpath resource does not exist.","triggerScenarios":"Calling withInitScript(\"cql/init.cql\") (or setting cassandra.container.init.script) where no file at that classpath path exists; wrong package-relative path (leading slash semantics), file excluded from the built jar/test resources, or typo in the path.","commonSituations":"Script placed in src/main/resources but tests run with a different classpath; Maven/Gradle resource filtering or excludes dropping .cql files; path missing the leading directories relative to classpath root; refactoring moved resources.","solutions":["Verify the resource exists on the test classpath at exactly the given path (getResource(\"/cql/init.cql\") != null)","Fix the path to be classpath-root relative and check case sensitivity (e.g. \"init.cql\" if at resources root)","Ensure build config copies the resource (check target/test-classes or build/resources for the file)","If the script lives on the filesystem instead, load it via a File path and use the appropriate copy mechanism rather than a classpath resource"],"exampleFix":"// before\nnew CassandraContainer<>(\"cassandra:5\").withInitScript(\"schema/init.cql\"); // file actually at resources/schema.cql\n// after\nnew CassandraContainer<>(\"cassandra:5\").withInitScript(\"schema.cql\"); // matches classpath root location","handlingStrategy":"validation","validationCode":"String script = \"schema.cql\";\nif (getClass().getClassLoader().getResource(script) == null)\n    throw new IllegalStateException(\"Init script not on classpath: \" + script);\ncassandra.withInitScript(script);","typeGuard":"boolean classpathResourceExists(String path) {\n    return Thread.currentThread().getContextClassLoader().getResource(\n        path.startsWith(\"/\") ? path.substring(1) : path) != null;\n}","tryCatchPattern":"try {\n    container.start();\n} catch (ScriptLoadException e) {\n    if (e.getMessage().contains(\"Resource not found\")) {\n        throw new IllegalStateException(\"Check withInitScript path: \" + container.getInitScript(), e);\n    }\n    throw e;\n}","preventionTips":["Assert resource existence in a unit test before integration runs","Keep init scripts under src/test/resources and reference paths relative to classpath root","Check build output (target/test-classes) to confirm resources are copied and not filtered"],"tags":["cassandra","classpath","init-script","resource-not-found"],"backgroundTag":"file-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"}