GoogleContainerTools/jib · error · NotDirectoryException

NotDirectoryException: <directory>

Error message

NotDirectoryException: <directory>

What it means

JavaContainerBuilder.addDirectory requires the given path to be a directory. After confirming existence, it checks Files.isDirectory(); if the path exists but is a regular file (or other non-directory), it throws NotDirectoryException naming the path. This prevents users from passing file paths where layer directories are expected.

Source

Thrown at jib-core/src/main/java/com/google/cloud/tools/jib/api/JavaContainerBuilder.java:683

      entrypoint.add("java");
      entrypoint.addAll(jvmFlags);
      entrypoint.add("-cp");
      entrypoint.add(classpathString);
      entrypoint.add(mainClass);
      jibContainerBuilder.setEntrypoint(entrypoint);
    }

    return jibContainerBuilder;
  }

  private JavaContainerBuilder addDirectory(
      List<PathPredicatePair> addedPaths, Path directory, Predicate<Path> filter)
      throws NoSuchFileException, NotDirectoryException {
    if (!Files.exists(directory)) {
      throw new NoSuchFileException(directory.toString());
    }
    if (!Files.isDirectory(directory)) {
      throw new NotDirectoryException(directory.toString());
    }
    addedPaths.add(new PathPredicatePair(directory, filter));
    return this;
  }

  private void addFileToLayer(
      Map<LayerType, FileEntriesLayer.Builder> layerBuilders,
      LayerType layerType,
      Path sourceFile,
      AbsoluteUnixPath pathInContainer) {
    if (!layerBuilders.containsKey(layerType)) {
      layerBuilders.put(layerType, FileEntriesLayer.builder());
    }
    Instant modificationTime = modificationTimeProvider.get(sourceFile, pathInContainer);
    layerBuilders.get(layerType).addEntry(sourceFile, pathInContainer, modificationTime);
  }

  private void addDirectoryContentsToLayer(

View on GitHub (pinned to fb949e2676)

Solutions

  1. Check the path with Files.isDirectory(path) before calling; point to the directory, not a file.
  2. If you want to add an individual file, use the file-oriented API (e.g. JavaContainerBuilder addToLayer / addFileToLayer path) instead of addDirectory.
  3. Remove or fix symlinks that resolve to files; ensure the path is the layer root directory.
  4. Unpack archives (jar/zip) to a directory if you intended to add their contents as a directory layer.

Example fix

// before
builder.addClasses(Paths.get("target/classes/com/app/Main.class"));
// after
Path classes = Paths.get("target/classes");
if (!Files.isDirectory(classes)) { throw new IllegalStateException("not a directory: " + classes); }
builder.addClasses(classes);
Defensive patterns

Strategy: validation

Validate before calling

if (Files.exists(dir) && !Files.isDirectory(dir)) throw new IllegalArgumentException("Not a directory: " + dir);

Type guard

boolean isDirectory(Path p) { return p != null && Files.isDirectory(p); }

Try / catch

try { builder.addClasses(dir); } catch (NotDirectoryException e) { log.error("Path is a file, not a directory: {}", e.getFile()); throw new BuildSetupException(e); }

Prevention

When it happens

Trigger: Calling addResources(Path)/addClasses(Path)/addDependencies(Path) (or addDirectory) with a Path that points to a regular file, e.g. passing an archive file or an individual class/resource file instead of its containing directory.

Common situations: Passing target/classes/SomeClass.class instead of target/classes; passing an app.jar where an exploded directory is expected; symlink resolution turning a directory path into a file; wrong variable holding a config file path.

Related errors


AI-assisted analysis of GoogleContainerTools/jib@fb949e2676 (2026-09-06). Data as JSON: /api/errors/a714c1168b717f6d. Report an issue: GitHub.