baomidou/mybatis-plus · error · IOException
File {} exists and is not a directory. Unable to create dire
Error message
File {} exists and is not a directory. Unable to create directory. What it means
FileUtils.forceMkdir throws IOException when the target path exists but is a regular file, not a directory. The generator needs a directory at that path to write generated files into, and it will not delete or overwrite an existing file, so it fails with the offending path in the message.
Source
Thrown at mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/util/FileUtils.java:47
* Makes a directory, including any necessary but nonexistent parent
* directories. If a file already exists with specified name but it is
* not a directory then an IOException is thrown.
* If the directory cannot be created (or does not already exist)
* then an IOException is thrown.
*
* @param directory directory to create, must not be {@code null}
* @throws NullPointerException if the directory is {@code null}
* @throws IOException if the directory cannot be created or the file already exists but is not a directory
*/
public static void forceMkdir(final File directory) throws IOException {
if (directory.exists()) {
if (!directory.isDirectory()) {
final String message =
"File "
+ directory
+ " exists and is "
+ "not a directory. Unable to create directory.";
throw new IOException(message);
}
} else {
if (!directory.mkdirs()) {
// Double-check that some other thread or process hasn't made
// the directory in the background
if (!directory.isDirectory()) {
final String message =
"Unable to create directory " + directory;
throw new IOException(message);
}
}
}
}
/**
* Makes any necessary but nonexistent parent directories for a given File. If the parent directory cannot be
* created then an IOException is thrown.
*View on GitHub (pinned to bf67d90747)
Solutions
- Check the path printed in the message: `ls -l <path>` will show a file where a directory is expected.
- Delete or rename the conflicting file, or point outputDir at a different path.
- Verify no trailing filename fragment (e.g. missing separator before a module name) turned the dir path into a file path.
Example fix
// before
globalConfig.setOutputDir("/proj/out"); // '/proj/out' is an existing FILE
// after
// remove or move the file, or choose a fresh dir
globalConfig.setOutputDir("/proj/out-gen"); Defensive patterns
Strategy: validation
Validate before calling
File dir = new File(outputDir);
if (dir.exists() && !dir.isDirectory()) {
throw new IllegalStateException("outputDir exists as a FILE, refusing to continue: " + dir);
} Prevention
- Use dedicated, empty-per-run output directories (target/generated-sources) instead of shared paths.
- Add a pre-flight check that each configured output path is absent or a directory.
- Avoid paths that other build steps also create as files.
When it happens
Trigger: outputDir (or any parent the generator must create, e.g. mapper xml output dir) pointing at an existing file — e.g. outputDir("src") where a file named 'src' exists, or a previous run's artifact was redirected onto the same path.
Common situations: Reusing an outputDir that a tool or teammate turned into a file; outputDir pointing to a path where a symlink/file was created by another build step; misconfigured globalConfig.setOutputDir with a filename accidentally appended.
Related errors
- Unable to create directory {}
- An exception occurred in the output file:
- Illegal directory {}
- %s 的名称转换结果为空,请检查是否配置问题
- `include` and `exclude` configurations are mutually exclusiv
AI-assisted analysis of baomidou/mybatis-plus@bf67d90747 (2026-08-14).
Data as JSON: /api/errors/0e9b5a119d2b5701.
Report an issue: GitHub.