{"record":{"id":"7842bd78ccf24af0","repo":"code4craft/webmagic","slug":"java-io-ioexception-7842bd","errorCode":null,"errorMessage":"java.io.IOException","messagePattern":"java\\.io\\.IOException","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"webmagic-scripts/src/main/java/us/codecraft/webmagic/scripts/ScriptProcessorBuilder.java","lineNumber":45,"sourceCode":"\n    private ScriptProcessorBuilder() {\n    }\n\n    public static ScriptProcessorBuilder custom() {\n        return new ScriptProcessorBuilder();\n    }\n\n    public ScriptProcessorBuilder language(Language language) {\n        this.language = language;\n        return this;\n    }\n\n    public ScriptProcessorBuilder scriptFromFile(String fileName) {\n        try {\n            InputStream resourceAsStream = new FileInputStream(fileName);\n            this.script = IOUtils.toString(resourceAsStream, Charset.defaultCharset());\n        } catch (IOException e) {\n            throw new IllegalArgumentException(e);\n        }\n        return this;\n    }\n\n    public ScriptProcessorBuilder scriptFromClassPathFile(String fileName) {\n        try {\n            InputStream resourceAsStream = ScriptProcessor.class.getClassLoader().getResourceAsStream(fileName);\n            this.script = IOUtils.toString(resourceAsStream, Charset.defaultCharset());\n        } catch (IOException e) {\n            throw new IllegalArgumentException(e);\n        }\n        return this;\n    }\n\n    public ScriptProcessorBuilder script(String script) {\n        this.script = script;\n        return this;\n    }","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/code4craft/webmagic/blob/67816a19d68a4fec4657bf1336227e046e251df2/webmagic-scripts/src/main/java/us/codecraft/webmagic/scripts/ScriptProcessorBuilder.java#L27-L63","documentation":"ScriptProcessorBuilder.scriptFromFile(fileName) reads the script from a file on the local filesystem (FileInputStream) into a String. Any IOException — file missing, no permission, path is a directory — is wrapped into IllegalArgumentException with message \"java.io.IOException: ...\". The builder call fails immediately.","triggerScenarios":"Calling scriptFromFile with a relative path that doesn't resolve from the process working directory, a non-existent file, a file without read permission, or a directory path.","commonSituations":"Using a relative path while the app runs from a different working directory (IDE vs. jar launch); forgetting to copy the script file into a container image; typos in the file name or extension; running under a service user lacking read access.","solutions":["Check the file exists and is readable before building: new File(fileName).canRead(), or use an absolute path.","Verify the process working directory resolves the relative path as expected (log new File(fileName).getAbsolutePath()).","Ensure the script file is included in the deployment artifact/container if packaged.","Fix filesystem permissions (chmod/chown) for the running user."],"exampleFix":"// before\nbuilder.scriptFromFile(\"scripts/my.js\");\n// after\nFile f = new File(\"scripts/my.js\");\nif (!f.canRead()) throw new IllegalStateException(\"Script missing: \" + f.getAbsolutePath());\nbuilder.scriptFromFile(f.getAbsolutePath());","handlingStrategy":"validation","validationCode":"java.io.File f = new java.io.File(fileName);\nif (!f.isFile() || !f.canRead()) {\n    throw new IllegalStateException(\"Script file missing/unreadable: \" + f.getAbsolutePath());\n}","typeGuard":null,"tryCatchPattern":"try {\n    builder.scriptFromFile(fileName);\n} catch (IllegalArgumentException e) {\n    if (e.getCause() instanceof java.io.FileNotFoundException) {\n        throw new IllegalStateException(\"Script file not found: \" + new File(fileName).getAbsolutePath(), e);\n    }\n    throw e;\n}","preventionTips":["Log File.getAbsolutePath() to verify the working-directory resolution of relative paths","Prefer absolute paths or classpath resources over relative file paths","Ensure script files are copied into containers/deployment artifacts","Check read permissions for the runtime service user"],"tags":["java","io","file-not-found","builder"],"backgroundTag":"file-not-found","analyzedSha":"67816a19d68a4fec4657bf1336227e046e251df2","analyzedAt":"2026-09-08T11:15:28.425Z","contentChangedAt":"2026-09-08T11:15:28.425Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}