code4craft/webmagic · error · IllegalArgumentException

java.io.IOException

Error message

java.io.IOException

What it means

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.

Solutions

  1. Check the file exists and is readable before building: new File(fileName).canRead(), or use an absolute path.
  2. Verify the process working directory resolves the relative path as expected (log new File(fileName).getAbsolutePath()).
  3. Ensure the script file is included in the deployment artifact/container if packaged.
  4. Fix filesystem permissions (chmod/chown) for the running user.

Example fix

// before
builder.scriptFromFile("scripts/my.js");
// after
File f = new File("scripts/my.js");
if (!f.canRead()) throw new IllegalStateException("Script missing: " + f.getAbsolutePath());
builder.scriptFromFile(f.getAbsolutePath());
Defensive patterns

Strategy: validation

Validate before calling

java.io.File f = new java.io.File(fileName);
if (!f.isFile() || !f.canRead()) {
    throw new IllegalStateException("Script file missing/unreadable: " + f.getAbsolutePath());
}

Try / catch

try {
    builder.scriptFromFile(fileName);
} catch (IllegalArgumentException e) {
    if (e.getCause() instanceof java.io.FileNotFoundException) {
        throw new IllegalStateException("Script file not found: " + new File(fileName).getAbsolutePath(), e);
    }
    throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of code4craft/webmagic@67816a19d6 (2026-09-08). Data as JSON: /api/errors/7842bd78ccf24af0. Report an issue: GitHub.

Appendix: source

Thrown at webmagic-scripts/src/main/java/us/codecraft/webmagic/scripts/ScriptProcessorBuilder.java:45

    private ScriptProcessorBuilder() {
    }

    public static ScriptProcessorBuilder custom() {
        return new ScriptProcessorBuilder();
    }

    public ScriptProcessorBuilder language(Language language) {
        this.language = language;
        return this;
    }

    public ScriptProcessorBuilder scriptFromFile(String fileName) {
        try {
            InputStream resourceAsStream = new FileInputStream(fileName);
            this.script = IOUtils.toString(resourceAsStream, Charset.defaultCharset());
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
        return this;
    }

    public ScriptProcessorBuilder scriptFromClassPathFile(String fileName) {
        try {
            InputStream resourceAsStream = ScriptProcessor.class.getClassLoader().getResourceAsStream(fileName);
            this.script = IOUtils.toString(resourceAsStream, Charset.defaultCharset());
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
        return this;
    }

    public ScriptProcessorBuilder script(String script) {
        this.script = script;
        return this;
    }

View on GitHub (pinned to 67816a19d6)