perwendel/spark · warning

Static file location has already been set

Error message

Static file location has already been set

What it means

Service.staticFileLocation logs 'Static file location has already been set' instead of applying the new folder: classpath static resources can only be configured once per service. The second call is silently ignored (warning only), so the developer's intended folder never takes effect.

Solutions

  1. Call staticFileLocation exactly once, before route mapping, with the single desired folder.
  2. Remove the duplicate staticFileLocation call found via the LOG.warn output.
  3. If multiple folders are needed, serve them via separate routes or an external location instead.
  4. Restructure initialization so service setup runs once (guard with an initialized flag).

Example fix

// before
service.staticFileLocation("/public");
service.staticFileLocation("/assets"); // ignored, warns
// after
service.staticFileLocation("/public"); // single authoritative root only
Defensive patterns

Strategy: validation

Validate before calling

if (!serviceInitialized) {
    service.staticFileLocation("/public");
    serviceInitialized = true;
}

Prevention

When it happens

Trigger: Calling staticFileLocation(...) a second time after a previous call already configured static resources; calling both staticFileLocation and externalStaticFileLocation in ways that collide on configuration state; framework hot-reload re-invoking the init code.

Common situations: Copy-pasted init code in multiple config classes/registries; switching from one resource folder to another without realizing the first call wins; running init twice due to servlet container restart semantics.

Understand the failure class

Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.

Related errors


AI-assisted analysis of perwendel/spark@1973e402f5 (2026-09-10). Data as JSON: /api/errors/deb1ebf5c252824c. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/spark/Service.java:346

        return this;
    }

    /**
     * Sets the folder in classpath serving static files. Observe: this method
     * must be called before all other methods.
     *
     * @param folder the folder in classpath.
     * @return the object with folder set
     */
    public synchronized Service staticFileLocation(String folder) {
        if (initialized && !isRunningFromServlet()) {
            throwBeforeRouteMappingException();
        }

        if (!staticFilesConfiguration.isStaticResourcesSet()) {
            staticFilesConfiguration.configure(folder);
        } else {
            LOG.warn("Static file location has already been set");
        }
        return this;
    }

    /**
     * Sets the external folder serving static files. <b>Observe: this method
     * must be called before all other methods.</b>
     *
     * @param externalFolder the external folder serving static files.
     * @return the object with external folder set
     */
    public synchronized Service externalStaticFileLocation(String externalFolder) {
        if (initialized && !isRunningFromServlet()) {
            throwBeforeRouteMappingException();
        }

        if (!staticFilesConfiguration.isExternalStaticResourcesSet()) {
            staticFilesConfiguration.configureExternal(externalFolder);

View on GitHub (pinned to 1973e402f5)