shwenzhang/AndResGuard · error · DirectoryException

file must be a directory

Error message

file must be a directory: ${dir}

What it means

FileDirectory's constructor throws DirectoryException("file must be a directory: <path>") when the given java.io.File exists but is not a directory (or the constructor is handed a file path instead of a folder path). The guard is dir.isDirectory().

Solutions

  1. Pass the directory, not a file: verify target.isDirectory() before constructing FileDirectory.
  2. If you have a file and need its containing directory, use file.getParentFile().
  3. Create the directory first (mkdirs) when it may not exist yet.

Example fix

// before
AbstractDirectory dir = new FileDirectory(new File("app.apk"));
// after
File outDir = new File("build/resguard");
if (!outDir.isDirectory() && !outDir.mkdirs()) { throw new IOException("cannot create " + outDir); }
AbstractDirectory dir = new FileDirectory(outDir);
Defensive patterns

Strategy: validation

Validate before calling

File f = new File(path); if (!f.isDirectory()) throw new IllegalArgumentException("not a directory: " + f);

Try / catch

try { new FileDirectory(f); } catch (DirectoryException e) { /* f is not a directory; fix path */ }

Prevention

When it happens

Trigger: new FileDirectory(file) where `file` points at a regular file (e.g. an .apk or .zip) instead of a folder; also on a stale path that no longer isDirectory().

Common situations: Passing the APK file itself where the unpacked/output directory was expected; mixing up input-file vs output-dir config values in a build script.

Related errors


AI-assisted analysis of shwenzhang/AndResGuard@e4df245d82 (2026-09-12). Data as JSON: /api/errors/0f1cc52f246be1e3. Report an issue: GitHub.

Appendix: source

Thrown at AndResGuard-core/src/main/java/com/tencent/mm/directory/FileDirectory.java:36

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;

public class FileDirectory extends AbstractDirectory {
  private File mDir;

  public FileDirectory(String dir) throws DirectoryException {
    this(new File(dir));
  }

  public FileDirectory(File dir) throws DirectoryException {
    super();
    if (!dir.isDirectory()) {
      throw new DirectoryException("file must be a directory: " + dir);
    }
    mDir = dir;
  }

  @Override
  protected AbstractDirectory createDirLocal(String name) throws DirectoryException {
    File dir = new File(generatePath(name));
    dir.mkdir();
    return new FileDirectory(dir);
  }

  @Override
  protected InputStream getFileInputLocal(String name) throws DirectoryException {
    try {
      return new FileInputStream(generatePath(name));
    } catch (FileNotFoundException e) {
      throw new DirectoryException(e);
    }

View on GitHub (pinned to e4df245d82)