baomidou/mybatis-plus · error · RuntimeException

An exception occurred in the output file:

Error message

An exception occurred in the output file: 

What it means

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.

Source

Thrown at mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/engine/AbstractTemplateEngine.java:253

            tableInfoList.forEach(tableInfo -> {
                Map<String, Object> objectMap = this.getObjectMap(config, tableInfo);
                Optional.ofNullable(config.getInjectionConfig()).ifPresent(t -> {
                    // 添加自定义属性
                    t.beforeOutputFile(tableInfo, objectMap);
                    // 输出自定义文件
                    outputCustomFile(t.getCustomFiles(), tableInfo, objectMap);
                });
                // entity
                outputEntity(tableInfo, objectMap);
                // mapper and xml
                outputMapper(tableInfo, objectMap);
                // service
                outputService(tableInfo, objectMap);
                // controller
                outputController(tableInfo, objectMap);
            });
        } catch (Exception e) {
            throw new RuntimeException("An exception occurred in the output file: ", e);
        }
        return this;
    }

    /**
     * 将模板转化成为字符串
     *
     * @param objectMap      渲染对象 MAP 信息
     * @param templateName   模板名称
     * @param templateString 模板字符串
     * @since 3.5.0
     */
    public abstract String writer(@NotNull Map<String, Object> objectMap, @NotNull String templateName, @NotNull String templateString) throws Exception;

    /**
     * 将模板转化成为文件
     *
     * @param objectMap    渲染对象 MAP 信息

View on GitHub (pinned to bf67d90747)

Solutions

  1. Look at the CAUSE exception (e.getCause()), not the wrapper message — it names the exact template, file path, or rendering error.
  2. If the cause is a template error, print the objectMap (many engines log available variables) and fix the template/variable mismatch.
  3. If the cause is IOException/AccessDenied, fix permissions on the outputDir and close programs holding generated files open.
  4. Re-run with the same config after fixing; verify the custom template paths in TemplateConfig/InjectionConfig actually exist on the classpath.

Example fix

// before
catch (Exception e) {
    log.error(e.getMessage()); // only shows generic wrapper text
}

// after
catch (Exception e) {
    Throwable root = e;
    while (root.getCause() != null) root = root.getCause();
    log.error("generation failed: {}", root.getMessage(), root);
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Before generating: make sure output dirs are writable
File out = new File(globalConfig.getOutputDir());
if (out.exists() && !out.isDirectory()) throw new IllegalStateException("outputDir is a file: " + out);
if (!out.exists() && !out.mkdirs()) throw new IllegalStateException("cannot create outputDir: " + out);

Try / catch

try {
    generator.generate();
} catch (RuntimeException e) {
    Throwable root = e;
    while (root.getCause() != null) root = root.getCause();
    log.error("template/output failed at root cause: {}", root.toString(), root);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of baomidou/mybatis-plus@bf67d90747 (2026-08-14). Data as JSON: /api/errors/a90147be84431715. Report an issue: GitHub.