SonarSource/sonarqube · error · IllegalStateException

"Failed to create temp directory - " + dir

Error message

"Failed to create temp directory - " + dir

What it means

DefaultTempFolder.newDir(name) creates a named subdirectory under the temp folder using FileUtils.forceMkdir. If an IOException occurs (permission denied, invalid name, parent missing, disk full) it is wrapped in this IllegalStateException including the target directory path. The library throws it because the caller expects a usable directory to be returned.

Solutions

  1. Use a filesystem-safe name (no path separators or illegal characters) for newDir
  2. Verify the DefaultTempFolder base directory still exists and is writable; recreate the folder if cleaned
  3. Check process permissions and disk space/quota on the temp filesystem
  4. Catch the IllegalStateException and fall back to Files.createTempDirectory on a known-writable path

Example fix

// before
File dir = tempFolder.newDir("sub/dir"); // contains path separator
// after
File dir = tempFolder.newDir("sub-dir");
Defensive patterns

Strategy: validation

Validate before calling

// validate dir name and base availability before newDir
if (name == null || name.isEmpty() || name.contains("/") || name.contains("\\")) {
  throw new IllegalArgumentException("invalid temp dir name: " + name);
}
if (!Files.isDirectory(tempBase) || !Files.isWritable(tempBase)) {
  throw new IllegalStateException("temp base missing or not writable: " + tempBase);
}

Try / catch

try {
  return tempFolder.newDir(name);
} catch (IllegalStateException e) {
  log.error("newDir({}) failed: {}", name, e.getCause());
  throw e;
}

Prevention

When it happens

Trigger: Calling newDir(name) or newDir(String) when the subdirectory cannot be created: name invalid for the filesystem, temp base deleted while running, or insufficient write permissions.

Common situations: Illegal characters in the requested dir name on Windows; another process cleaning the temp folder concurrently; service accounts lacking write access to java.io.tmpdir; disk quota exceeded.

Understand the failure class

Background: mkdir permission denied (EACCES): failed to create directory errors explained — this error's family across 32 libraries.

Related errors


AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09). Data as JSON: /api/errors/70788dca506bb688. Report an issue: GitHub.

Appendix: source

Thrown at sonar-plugin-api-impl/src/main/java/org/sonar/api/impl/utils/DefaultTempFolder.java:72

  public File newDir() {
    return createTempDir(tempDir.toPath()).toFile();
  }

  private static Path createTempDir(Path baseDir) {
    try {
      return Files.createTempDirectory(baseDir, null);
    } catch (IOException e) {
      throw new IllegalStateException("Failed to create temp directory", e);
    }
  }

  @Override
  public File newDir(String name) {
    File dir = new File(tempDir, name);
    try {
      FileUtils.forceMkdir(dir);
    } catch (IOException e) {
      throw new IllegalStateException("Failed to create temp directory - " + dir, e);
    }
    return dir;
  }

  @Override
  public File newFile() {
    return newFile(null, null);
  }

  @Override
  public File newFile(@Nullable String prefix, @Nullable String suffix) {
    return createTempFile(tempDir.toPath(), prefix, suffix).toFile();
  }

  private static Path createTempFile(Path baseDir, @Nullable String prefix, @Nullable String suffix) {
    try {
      return Files.createTempFile(baseDir, prefix, suffix);
    } catch (IOException e) {

View on GitHub (pinned to 184c821202)