alibaba/arthas · error · IOException
Directory '{parent}' could not be created
Error message
Directory '{parent}' could not be created What it means
FileUtils.openOutputStream(file, append): when the target file does NOT exist and has a parent directory, it calls parent.mkdirs(). If mkdirs() returns false AND parent.isDirectory() is also false (i.e. creation failed and it still isn't a directory), it throws IOException("Directory '<parent>' could not be created").
Source
Thrown at common/src/main/java/com/taobao/arthas/common/FileUtils.java:125
* @return a new {@link FileOutputStream} for the specified file
* @throws IOException if the file object is a directory
* @throws IOException if the file cannot be written to
* @throws IOException if a parent directory needs creating but that fails
* @since 2.1
*/
public static FileOutputStream openOutputStream(final File file, final boolean append) throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File '" + file + "' exists but is a directory");
}
if (!file.canWrite()) {
throw new IOException("File '" + file + "' cannot be written to");
}
} else {
final File parent = file.getParentFile();
if (parent != null) {
if (!parent.mkdirs() && !parent.isDirectory()) {
throw new IOException("Directory '" + parent + "' could not be created");
}
}
}
return new FileOutputStream(file, append);
}
/**
* Reads the contents of a file into a byte array.
* The file is always closed.
*
* @param file the file to read, must not be {@code null}
* @return the file contents, never {@code null}
* @throws IOException in case of an I/O error
* @since 1.1
*/
public static byte[] readFileToByteArray(final File file) throws IOException {
InputStream in = null;
try {View on GitHub (pinned to 21cf2e9ba5)
Solutions
- Pre-create the parent directory with correct ownership before running.
- Grant write permission on the ancestor directory so mkdirs can succeed.
- Choose an output path under a writable location (e.g. /tmp, the user's home).
- Remove any non-directory entry conflicting with the parent path.
Example fix
# before
FileUtils.openOutputStream(new File("/opt/arthas/out/trace.log"), false); // /opt/arthas/out absent, no perms
# after
mkdir -p /opt/arthas/out && chown app:app /opt/arthas/out
# then the call succeeds Defensive patterns
Strategy: validation
Validate before calling
File parent = file.getParentFile();
if (parent != null && !parent.exists() && !parent.mkdirs()) {
throw new IOException("cannot create parent dir: " + parent);
} Type guard
static boolean parentReady(File f) {
File p = f == null ? null : f.getParentFile();
return p == null || p.isDirectory() || p.mkdirs();
} Try / catch
try {
return FileUtils.openOutputStream(file, append);
} catch (IOException e) {
if (e.getMessage().contains("could not be created")) {
// pre-create parent with correct perms, then retry
} else throw e;
} Prevention
- Pre-create and chown output directories in provisioning.
- Use writable locations (home, /tmp) for runtime outputs.
- Remove conflicting non-directory entries along the parent chain.
When it happens
Trigger: Writing to a file whose parent path does not exist and cannot be created — insufficient permission on the grandparent, a non-directory entry blocking the path, or a read-only filesystem.
Common situations: Output directory under /var/run or /opt owned by root while the process runs unprivileged; a file (not dir) already exists where a directory is expected along the parent chain; read-only/root filesystem in a constrained container; disk full or quota exceeded.
Related errors
- File '{file}' cannot be written to
- File '{file}' exists but is a directory
- Bad zip entry: {currentEntry}
- Failed to create directory within ${TEMP_DIR_ATTEMPTS} attem
- Failed to prepare MCP upload directory
AI-assisted analysis of alibaba/arthas@21cf2e9ba5 (2026-08-14).
Data as JSON: /api/errors/370c1d4d2b2aa15e.
Report an issue: GitHub.