{"record":{"id":"a90147be84431715","repo":"baomidou/mybatis-plus","slug":"an-exception-occurred-in-the-output-file","errorCode":null,"errorMessage":"An exception occurred in the output file: ","messagePattern":"An exception occurred in the output file: ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/engine/AbstractTemplateEngine.java","lineNumber":253,"sourceCode":"            tableInfoList.forEach(tableInfo -> {\n                Map<String, Object> objectMap = this.getObjectMap(config, tableInfo);\n                Optional.ofNullable(config.getInjectionConfig()).ifPresent(t -> {\n                    // 添加自定义属性\n                    t.beforeOutputFile(tableInfo, objectMap);\n                    // 输出自定义文件\n                    outputCustomFile(t.getCustomFiles(), tableInfo, objectMap);\n                });\n                // entity\n                outputEntity(tableInfo, objectMap);\n                // mapper and xml\n                outputMapper(tableInfo, objectMap);\n                // service\n                outputService(tableInfo, objectMap);\n                // controller\n                outputController(tableInfo, objectMap);\n            });\n        } catch (Exception e) {\n            throw new RuntimeException(\"An exception occurred in the output file: \", e);\n        }\n        return this;\n    }\n\n    /**\n     * 将模板转化成为字符串\n     *\n     * @param objectMap      渲染对象 MAP 信息\n     * @param templateName   模板名称\n     * @param templateString 模板字符串\n     * @since 3.5.0\n     */\n    public abstract String writer(@NotNull Map<String, Object> objectMap, @NotNull String templateName, @NotNull String templateString) throws Exception;\n\n    /**\n     * 将模板转化成为文件\n     *\n     * @param objectMap    渲染对象 MAP 信息","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/baomidou/mybatis-plus/blob/bf67d907478c724120bf76292da54abf9e73c2b3/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/engine/AbstractTemplateEngine.java#L235-L271","documentation":"AbstractTemplateEngine.outputAll wraps ANY exception thrown while rendering/writing the per-table output files (custom files, entity, mapper+xml, service, controller) into a RuntimeException with this prefix. The real failure (template exception, I/O error, null in the model) is always the cause, so the message alone is intentionally generic.","triggerScenarios":"Calling generator.generate() when a template cannot be rendered (missing variable, bad custom template path), the output directory is not writable, a file is locked by another process (Windows), or objectMap preparation throws inside any outputXxx method.","commonSituations":"Custom template files referencing variables not present in the objectMap; running the generator in an IDE/CI where the output dir is read-only; antivirus or an open explorer window locking a generated .java file on Windows; classpath template conflicts after upgrading the generator version.","solutions":["Look at the CAUSE exception (e.getCause()), not the wrapper message — it names the exact template, file path, or rendering error.","If the cause is a template error, print the objectMap (many engines log available variables) and fix the template/variable mismatch.","If the cause is IOException/AccessDenied, fix permissions on the outputDir and close programs holding generated files open.","Re-run with the same config after fixing; verify the custom template paths in TemplateConfig/InjectionConfig actually exist on the classpath."],"exampleFix":"// before\ncatch (Exception e) {\n    log.error(e.getMessage()); // only shows generic wrapper text\n}\n\n// after\ncatch (Exception e) {\n    Throwable root = e;\n    while (root.getCause() != null) root = root.getCause();\n    log.error(\"generation failed: {}\", root.getMessage(), root);\n}","handlingStrategy":"try-catch","validationCode":"// Before generating: make sure output dirs are writable\nFile out = new File(globalConfig.getOutputDir());\nif (out.exists() && !out.isDirectory()) throw new IllegalStateException(\"outputDir is a file: \" + out);\nif (!out.exists() && !out.mkdirs()) throw new IllegalStateException(\"cannot create outputDir: \" + out);","typeGuard":null,"tryCatchPattern":"try {\n    generator.generate();\n} catch (RuntimeException e) {\n    Throwable root = e;\n    while (root.getCause() != null) root = root.getCause();\n    log.error(\"template/output failed at root cause: {}\", root.toString(), root);\n}","preventionTips":["Always unwrap to the root cause before reporting; the wrapper message carries no detail.","Test custom templates on one table first (include a single table) before a full run.","Keep outputDir inside the project/build directory where write access is guaranteed."],"tags":["mybatis-plus","code-generator","template","io","wrapper-exception"],"backgroundTag":null,"analyzedSha":"bf67d907478c724120bf76292da54abf9e73c2b3","analyzedAt":"2026-08-14T15:17:09.543Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}