spring-projects/spring-boot · error · ReportableException

{Directory|File} '{file.getName()}' already exists. Use --fo

Error message

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

What it means

During zip extraction (extractFromStream), if a target File for an entry already exists on disk and the caller did not pass --force, generation aborts to avoid clobbering an existing file or directory. The message tells you to either allow overwrite or choose a different output location.

Source

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

			fixExecutableFlag(outputDirectory, "gradlew");
			Log.info("Project extracted to '" + outputDirectory.getAbsolutePath() + "'");
		}
	}

	private void extractFromStream(ZipInputStream zipStream, boolean overwrite, File outputDirectory)
			throws IOException {
		ZipEntry entry = zipStream.getNextEntry();
		String canonicalOutputPath = outputDirectory.getCanonicalPath() + File.separator;
		while (entry != null) {
			File file = new File(outputDirectory, entry.getName());
			String canonicalEntryPath = file.getCanonicalPath();
			if (!canonicalEntryPath.startsWith(canonicalOutputPath)) {
				throw new ReportableException("Entry '" + entry.getName() + "' would be written to '"
						+ canonicalEntryPath + "'. This is outside the output location of '" + canonicalOutputPath
						+ "'. Verify your target server configuration.");
			}
			if (file.exists() && !overwrite) {
				throw new ReportableException((file.isDirectory() ? "Directory" : "File") + " '" + file.getName()
						+ "' 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) {

View on GitHub (pinned to 5b2dbdbb8b)

Solutions

  1. Pass --force to overwrite existing entries: `spring init --force --output <dir>/`.
  2. Choose a fresh/empty output directory.
  3. Clean the target directory before re-running if partial extraction is undesirable.

Example fix

// before
$ spring init --output myapp/
  -> File 'pom.xml' already exists...
// after
$ spring init --force --output myapp/
Defensive patterns

Strategy: validation

Validate before calling

File out = (request.getOutput() != null) ? new File(request.getOutput()) : new File(System.getProperty("user.dir"));
if (!force && out.exists() && out.isDirectory()) {
    try (Stream<Path> s = Files.list(out.toPath())) {
        if (s.findAny().isPresent()) {
            throw new IllegalStateException("Output dir not empty; pass --force or use a fresh dir.");
        }
    }
}

Type guard

boolean safeToExtract = force || !outputDirectory.exists()
    || (outputDirectory.isDirectory() && directoryIsEmpty(outputDirectory));

Try / catch

// Prefer pre-validation. If you must wrap:
try { generator.generateProject(request, force); }
catch (ReportableException ex) {
    if (ex.getMessage().contains("already exists") && !force) {
        generator.generateProject(request, /*force*/ true); // user confirmed overwrite
    } else throw ex;
}

Prevention

When it happens

Trigger: Running `spring init --output <dir>/` (or letting output default to the current directory) when <dir> (or the current dir) already contains a file or directory matching a zip entry name, without --force. The check at line 123 fires for the first conflicting entry.

Common situations: Re-running `spring init` into the same directory twice; extracting into a non-empty folder; a previous failed run left partial files behind.

Related errors


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