{"record":{"id":"c286cd2c5ccac5ad","repo":"spring-projects/spring-boot","slug":"file-already-exists-use-force-if-you-want","errorCode":null,"errorMessage":"File '{}' already exists. Use --force if you want to overwrite or specify an alternate location.","messagePattern":"File '(.+?)' 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":143,"sourceCode":"\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) {\n\t\t\t\tthrow new ReportableException(\n\t\t\t\t\t\t\"File '\" + outputFile.getName() + \"' already exists. Use --force if you want to \"\n\t\t\t\t\t\t\t\t+ \"overwrite or specify an alternate location.\");\n\t\t\t}\n\t\t\tif (!outputFile.delete()) {\n\t\t\t\tthrow new ReportableException(\"Failed to delete existing file \" + outputFile.getPath());\n\t\t\t}\n\t\t}\n\t\tbyte[] content = entity.getContent();\n\t\tAssert.state(content != null, \"'content' must not be null\");\n\t\tFileCopyUtils.copy(content, outputFile);\n\t\tLog.info(\"Content saved to '\" + output + \"'\");\n\t}\n\n\tprivate void fixExecutableFlag(File dir, String fileName) {\n\t\tFile f = new File(dir, fileName);\n\t\tif (f.exists()) {\n\t\t\tf.setExecutable(true, false);\n\t\t}","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/spring-projects/spring-boot/blob/270dfe353fb830fd69b823a8a859287ff103854b/cli/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerator.java#L125-L161","documentation":"Thrown by ProjectGenerator.writeProject when the single-file output target already exists and --force was not supplied. Unlike [93] (per-entry during extraction), this guards the top-level output file path used when writing the project as a single archive.","triggerScenarios":"Writing the project to a file (no --extract) whose path already exists, without --force.","commonSituations":"Re-running 'spring init --output app.zip' twice; the output file name collides with a pre-existing file.","solutions":["Pass --force to overwrite the file","Choose a different --output file name","Delete or rename the existing file first"],"exampleFix":"// before\n$ spring init --output app.zip  # app.zip exists\n// after\n$ spring init --output app.zip --force","handlingStrategy":"validation","validationCode":"// Before writing, check the output file and decide explicitly.\njava.io.File out = new java.io.File(System.getProperty(\"user.dir\"), request.getOutput());\nif (out.exists() && !force) {\n    throw new IllegalStateException(\n        \"Output exists: \" + out + \" - pass --force or pick another name\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    generator.generateProject(request, force);\n} catch (ReportableException ex) {\n    if (ex.getMessage().startsWith(\"File '\") && ex.getMessage().contains(\"already exists\")) {\n        Log.error(\"Output file exists - rerun with --force or a different --output\");\n    }\n    throw ex;\n}","preventionTips":["Use a unique --output name per run (timestamp, project name)","Pass --force only when overwriting intentionally","Clean stale output files in CI between runs"],"tags":["initializr","file-output","overwrite","validation","spring-boot-cli"],"backgroundTag":null,"analyzedSha":"270dfe353fb830fd69b823a8a859287ff103854b","analyzedAt":"2026-08-11T19:42:06.541Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}