spring-projects/spring-boot · error · ReportableException

File '{outputFile.getName()}' already exists. Use --force if

Error message

File '{outputFile.getName()}' already exists. Use --force if you want to overwrite or specify an alternate location.

What it means

In ProjectGenerator.writeProject (single-file write path), if the resolved outputFile already exists and overwrite (--force) is false, generation aborts with this message. It mirrors [86] but applies to the non-extracted case where the whole project is written as one file (e.g. a .zip).

Source

Thrown at cli/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerator.java:143

						+ "' already exists. Use --force if you want to overwrite or "
						+ "specify an alternate location.");
			}
			if (!entry.isDirectory()) {
				FileCopyUtils.copy(StreamUtils.nonClosing(zipStream), new FileOutputStream(file));
			}
			else {
				file.mkdir();
			}
			zipStream.closeEntry();
			entry = zipStream.getNextEntry();
		}
	}

	private void writeProject(ProjectGenerationResponse entity, String output, boolean overwrite) throws IOException {
		File outputFile = new File(System.getProperty("user.dir"), output);
		if (outputFile.exists()) {
			if (!overwrite) {
				throw new ReportableException(
						"File '" + outputFile.getName() + "' already exists. Use --force if you want to "
								+ "overwrite or specify an alternate location.");
			}
			if (!outputFile.delete()) {
				throw new ReportableException("Failed to delete existing file " + outputFile.getPath());
			}
		}
		byte[] content = entity.getContent();
		Assert.state(content != null, "'content' must not be null");
		FileCopyUtils.copy(content, outputFile);
		Log.info("Content saved to '" + output + "'");
	}

	private void fixExecutableFlag(File dir, String fileName) {
		File f = new File(dir, fileName);
		if (f.exists()) {
			f.setExecutable(true, false);
		}

View on GitHub (pinned to 5b2dbdbb8b)

Solutions

  1. Pass --force to overwrite the existing file.
  2. Specify a different --output filename.
  3. Delete the existing file before re-running.

Example fix

// before
$ spring init --output demo.zip
  -> File 'demo.zip' already exists...
// after
$ spring init --force --output demo.zip
Defensive patterns

Strategy: validation

Validate before calling

Path p = Path.of(System.getProperty("user.dir"), request.getOutput());
if (!force && Files.exists(p)) {
    throw new IllegalStateException("Output file exists: " + p + " (use --force or rename)");
}

Type guard

boolean canWrite = force || !Files.exists(outputFile.toPath());

Try / catch

try { generator.generateProject(request, force); }
catch (ReportableException ex) {
    if (ex.getMessage().startsWith("File '") && ex.getMessage().contains("already exists") && !force) {
        request.setOutput(request.getOutput().replace(".zip", "-" + System.currentTimeMillis() + ".zip"));
        generator.generateProject(request, force);
    } else throw ex;
}

Prevention

When it happens

Trigger: Calling `spring init --output myapp.zip` when myapp.zip already exists in the working directory, without --force. Line 141 sees outputFile.exists() and line 142 !overwrite triggers the exception.

Common situations: Re-generating to the same output filename; CI runs that don't clean prior artifacts; iterating on init parameters while keeping a fixed output name.

Related errors


AI-assisted analysis of spring-projects/spring-boot@5b2dbdbb8b (2026-08-04). Data as JSON: /data/errors/277f4da43f44f880.json. Report an issue: GitHub.