stanfordnlp/CoreNLP · error · IOException
Could not create directory <tgtDir.getAbsolutePath()>, as a…
Error message
Could not create directory <tgtDir.getAbsolutePath()>, as a file already exists at that path.
What it means
IOUtils.ensureDir(File) throws this IOException when the target path already exists but is a regular file (or other non-directory), so the directory cannot be created there. The message includes the absolute path of the conflicting file.
Solutions
- Delete or rename the existing file at that path, or choose a different directory path, then call ensureDir again.
- Before calling, check existence and kind: if the path exists and is not a directory, fail early with a clear message.
- Fix the configuration/argument that supplies the path so it refers to the intended directory location.
Example fix
// before
IOUtils.ensureDir(new File(outputPath));
// after
File dir = new File(outputPath);
if (dir.exists() && !dir.isDirectory()) {
throw new IllegalStateException("Path is a file, expected directory: " + dir.getAbsolutePath());
}
IOUtils.ensureDir(dir); Defensive patterns
Strategy: validation
Validate before calling
File dir = new File(path);
if (dir.exists() && !dir.isDirectory())
throw new IllegalStateException("Path exists but is not a directory: " + dir.getAbsolutePath()); Try / catch
try {
IOUtils.ensureDir(dir);
} catch (IOException e) {
if (new File(path).isFile()) {
throw new IllegalStateException("A file occupies the directory path: " + path);
}
throw e;
} Prevention
- Check path kind (file vs directory) before ensureDir.
- Keep lock/temp files out of directory-path locations.
- Beware symlinks: isDirectory() follows them.
When it happens
Trigger: Calling IOUtils.ensureDir(tgtDir) where tgtDir.exists() is true and tgtDir.isDirectory() is false — i.e. a regular file, symlink-to-file, or special file occupies the intended directory path.
Common situations: A previous run created the path as a file (e.g. a lockfile or a mistakenly written output file); a config value for a directory path accidentally points at a file; a symlink points to a file; Docker bind-mounts creating a file where a directory was expected.
Understand the failure class
Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.
Related errors
- cp: cannot copy to directory
- Could not open temporary feature index file for writing.
- Could not open temporary feature index file for reading.
- Could not create directory <tgtDir.getAbsolutePath()>
- cp: omitting directory
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/407335c7526da516.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/io/IOUtils.java:1742
return new File("/"+machineName+"/scr1/"+username);
} catch (Exception e) {
return new File("./scr/"); // default scratch
}
}
/**
* Given a filepath, makes sure a directory exists there. If not, creates and returns it.
* Same as ENSURE-DIRECTORY in CL.
*
* @param tgtDir The directory that you wish to ensure exists
* @throws IOException If directory can't be created, is an existing file, or for other reasons
*/
public static File ensureDir(File tgtDir) throws IOException {
if (tgtDir.exists()) {
if (tgtDir.isDirectory()) {
return tgtDir;
} else {
throw new IOException("Could not create directory "+tgtDir.getAbsolutePath()+", as a file already exists at that path.");
}
} else {
if ( ! tgtDir.mkdirs()) {
throw new IOException("Could not create directory "+tgtDir.getAbsolutePath());
}
return tgtDir;
}
}
/**
* Given a filepath, delete all files in the directory recursively
* @param dir Directory from which to delete files
* @return {@code true} if the deletion is successful, {@code false} otherwise
*/
public static boolean deleteDirRecursively(File dir) {
if (dir.isDirectory()) {
for (File f : dir.listFiles()) {
boolean success = deleteDirRecursively(f);View on GitHub (pinned to 1b7edd19c4)