{"id":"277f4da43f44f880","repo":"spring-projects/spring-boot","slug":"file-outputfile-getname-already-exists-use","errorCode":null,"errorMessage":"File '{outputFile.getName()}' 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/5b2dbdbb8be64415eb6552f81ff7c449c8d251e6/cli/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerator.java#L125-L161","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass --force to overwrite the existing file.","Specify a different --output filename.","Delete the existing file before re-running."],"exampleFix":"// before\n$ spring init --output demo.zip\n  -> File 'demo.zip' already exists...\n// after\n$ spring init --force --output demo.zip","handlingStrategy":"validation","validationCode":"Path p = Path.of(System.getProperty(\"user.dir\"), request.getOutput());\nif (!force && Files.exists(p)) {\n    throw new IllegalStateException(\"Output file exists: \" + p + \" (use --force or rename)\");\n}","typeGuard":"boolean canWrite = force || !Files.exists(outputFile.toPath());","tryCatchPattern":"try { generator.generateProject(request, force); }\ncatch (ReportableException ex) {\n    if (ex.getMessage().startsWith(\"File '\") && ex.getMessage().contains(\"already exists\") && !force) {\n        request.setOutput(request.getOutput().replace(\".zip\", \"-\" + System.currentTimeMillis() + \".zip\"));\n        generator.generateProject(request, force);\n    } else throw ex;\n}","preventionTips":["Parameterize --output with a timestamp/build id for repeatable runs.","Pass --force only when clobbering is acceptable (and the file is deletable).","In CI, clean output artifacts before the init step."],"tags":["spring-boot-cli","initializr","project-generation","file-io"],"analyzedSha":"5b2dbdbb8be64415eb6552f81ff7c449c8d251e6","analyzedAt":"2026-08-04T18:53:14.967Z","schemaVersion":2}