SonarSource/sonarqube · error · IllegalStateException

Can not open file

Error message

Can not open file 

What it means

Files2.openOutputStream(File, boolean) opens a FileOutputStream after ensuring parent directories exist and the file is writable, translating any IOException into an IllegalStateException with the message "Can not open file " plus the path. It covers both the pre-checks in openOutputStreamOrThrowIOE (including the 'Directory could not be created' case) and the actual FileOutputStream construction.

Source

Thrown at server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/util/Files2.java:177

   * Opens a {@link FileOutputStream} for the specified file, checking and
   * creating the parent directory if it does not exist.
   * <p>
   * The parent directory will be created if it does not exist.
   * The file will be created if it does not exist.
   *
   * @param file  the file to open for output, must not be {@code null}
   * @param append if {@code true}, then bytes will be added to the
   * end of the file rather than overwriting
   * @return a new {@link FileOutputStream} for the specified file
   * @throws IllegalStateException if the specified file is a directory
   * @throws IllegalStateException if the file can not be written to
   * @throws IllegalStateException if a parent directory can not be created
   */
  public FileOutputStream openOutputStream(File file, boolean append) {
    try {
      return openOutputStreamOrThrowIOE(file, append);
    } catch (IOException e) {
      throw new IllegalStateException("Can not open file " + file, e);
    }
  }

  /**
   * Opens a {@link FileInputStream} for the specified file, providing better
   * error messages than simply calling {@code new FileInputStream(file)}.
   *
   * @param file  the file to open, must not be {@code null}
   * @return a new {@link FileInputStream} for the specified file
   * @throws IOException if the file does not exist
   * @throws IOException if the specified file is a directory
   * @throws IOException if the file can not be read
   */
  public FileInputStream openInputStreamOrThrowIOE(File file) throws IOException {
    checkOrThrowIOE(!file.isDirectory(), "File %s exists but is a directory", file);
    checkOrThrowIOE(file.exists(), "File %s does not exist", file);
    checkOrThrowIOE(file.canRead(), "File %s can not be read", file);
    return new FileInputStream(file);

View on GitHub (pinned to 184c821202)

Solutions

  1. Verify the target path is a writable file location and not a directory
  2. Check filesystem permissions for the CE process user on the target and its parents
  3. Free disk space / raise quota on the target volume
  4. Fix the configured output or work directory path and retry

Example fix

// before
files2.openOutputStream(new File("/data/dump"), false); // path is a directory
// after
File out = new File("/data/dump/metadata.pb");
if (out.isDirectory()) {
  throw new IllegalArgumentException("Expected a file path: " + out);
}
files2.openOutputStream(out, false);
Defensive patterns

Strategy: validation

Validate before calling

if (file.isDirectory()) throw new IllegalArgumentException("Expected file, got dir: " + file);
if (file.exists() && !file.canWrite()) throw new IllegalArgumentException("Not writable: " + file);
File p = file.getParentFile();
if (p != null && !p.isDirectory() && !p.mkdirs()) throw new IllegalArgumentException("Cannot create " + p);

Try / catch

try {
  FileOutputStream out = files2.openOutputStream(file, append);
} catch (IllegalStateException e) {
  LOGGER.error("Open for write failed on {}: {}", file, e.getCause(), e);
}

Prevention

When it happens

Trigger: openOutputStreamOrThrowIOE throws IOException — file exists but is a directory, file not writable, parent directory cannot be created, or new FileOutputStream fails (permission denied, disk full, path invalid).

Common situations: Dump/report output path points at a directory instead of a file; CE user lacks write permission; disk quota or full volume; misconfigured work directory path.

Understand the failure class

Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.

Related errors


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