{"id":"ad31fd79321ed5d0","repo":"spring-projects/spring-boot","slug":"directory-file-file-getname-already-exists","errorCode":null,"errorMessage":"{Directory|File} '{file.getName()}' already exists. Use --force if you want to overwrite or specify an alternate location.","messagePattern":"(.+?) '(.+?)' already exists\\. Use --force if you want to overwrite or specify an alternate location\\.","errorType":"exception","errorClass":"ReportableException","httpStatus":null,"severity":"error","filePath":"cli/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerator.java","lineNumber":124,"sourceCode":"\t\t\tfixExecutableFlag(outputDirectory, \"gradlew\");\n\t\t\tLog.info(\"Project extracted to '\" + outputDirectory.getAbsolutePath() + \"'\");\n\t\t}\n\t}\n\n\tprivate void extractFromStream(ZipInputStream zipStream, boolean overwrite, File outputDirectory)\n\t\t\tthrows IOException {\n\t\tZipEntry entry = zipStream.getNextEntry();\n\t\tString canonicalOutputPath = outputDirectory.getCanonicalPath() + File.separator;\n\t\twhile (entry != null) {\n\t\t\tFile file = new File(outputDirectory, entry.getName());\n\t\t\tString canonicalEntryPath = file.getCanonicalPath();\n\t\t\tif (!canonicalEntryPath.startsWith(canonicalOutputPath)) {\n\t\t\t\tthrow new ReportableException(\"Entry '\" + entry.getName() + \"' would be written to '\"\n\t\t\t\t\t\t+ canonicalEntryPath + \"'. This is outside the output location of '\" + canonicalOutputPath\n\t\t\t\t\t\t+ \"'. Verify your target server configuration.\");\n\t\t\t}\n\t\t\tif (file.exists() && !overwrite) {\n\t\t\t\tthrow new ReportableException((file.isDirectory() ? \"Directory\" : \"File\") + \" '\" + file.getName()\n\t\t\t\t\t\t+ \"' already exists. Use --force if you want to overwrite or \"\n\t\t\t\t\t\t+ \"specify an alternate location.\");\n\t\t\t}\n\t\t\tif (!entry.isDirectory()) {\n\t\t\t\tFileCopyUtils.copy(StreamUtils.nonClosing(zipStream), new FileOutputStream(file));\n\t\t\t}\n\t\t\telse {\n\t\t\t\tfile.mkdir();\n\t\t\t}\n\t\t\tzipStream.closeEntry();\n\t\t\tentry = zipStream.getNextEntry();\n\t\t}\n\t}\n\n\tprivate void writeProject(ProjectGenerationResponse entity, String output, boolean overwrite) throws IOException {\n\t\tFile outputFile = new File(System.getProperty(\"user.dir\"), output);\n\t\tif (outputFile.exists()) {\n\t\t\tif (!overwrite) {","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/spring-projects/spring-boot/blob/5b2dbdbb8be64415eb6552f81ff7c449c8d251e6/cli/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerator.java#L106-L142","documentation":"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.","triggerScenarios":"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.","commonSituations":"Re-running `spring init` into the same directory twice; extracting into a non-empty folder; a previous failed run left partial files behind.","solutions":["Pass --force to overwrite existing entries: `spring init --force --output <dir>/`.","Choose a fresh/empty output directory.","Clean the target directory before re-running if partial extraction is undesirable."],"exampleFix":"// before\n$ spring init --output myapp/\n  -> File 'pom.xml' already exists...\n// after\n$ spring init --force --output myapp/","handlingStrategy":"validation","validationCode":"File out = (request.getOutput() != null) ? new File(request.getOutput()) : new File(System.getProperty(\"user.dir\"));\nif (!force && out.exists() && out.isDirectory()) {\n    try (Stream<Path> s = Files.list(out.toPath())) {\n        if (s.findAny().isPresent()) {\n            throw new IllegalStateException(\"Output dir not empty; pass --force or use a fresh dir.\");\n        }\n    }\n}","typeGuard":"boolean safeToExtract = force || !outputDirectory.exists()\n    || (outputDirectory.isDirectory() && directoryIsEmpty(outputDirectory));","tryCatchPattern":"// Prefer pre-validation. If you must wrap:\ntry { generator.generateProject(request, force); }\ncatch (ReportableException ex) {\n    if (ex.getMessage().contains(\"already exists\") && !force) {\n        generator.generateProject(request, /*force*/ true); // user confirmed overwrite\n    } else throw ex;\n}","preventionTips":["Always extract into a fresh or empty directory, or pass --force deliberately.","In CI, clean the output dir before each `spring init` run.","Avoid reusing the current working directory for extraction."],"tags":["spring-boot-cli","initializr","project-generation","file-io"],"analyzedSha":"5b2dbdbb8be64415eb6552f81ff7c449c8d251e6","analyzedAt":"2026-08-04T18:53:14.967Z","schemaVersion":2}