SonarSource/sonarqube · error · IllegalStateException

format(CAN_NOT_LOAD_FROM_CLASSPATH, filePath)

Error message

format(CAN_NOT_LOAD_FROM_CLASSPATH, filePath)

What it means

MetadataLoader.getParamFromFile reads a single-line value from a resource on the classpath (e.g. sonar-api-version.txt / sq-version-eol files) using system.getResource. If opening or reading the resource stream throws IOException, it wraps it in an IllegalStateException with a CAN_NOT_LOAD_FROM_CLASSPATH message identifying the file path.

Solutions

  1. Verify the resource file exists in the jar/classpath and is not stripped by maven-shade-plugin/assembly excludes.
  2. Check the IOException cause in the stack trace to identify the underlying stream failure.
  3. Ensure the plugin runs on a standard SonarQube runtime rather than a repackaged environment.

Example fix

// packaging fix (maven-shade-plugin)
// before
<filters>
  <filter><artifact>*:*</artifact>
    <excludes><exclude>sonar-api-version.txt</exclude></excludes>
  </filter>
</filters>
// after: remove the exclude so the metadata file ships in the jar
Defensive patterns

Strategy: try-catch

Validate before calling

// Java
URL url = system.getResource("/sonar-api-version.txt");
if (url == null) throw new IllegalStateException("Metadata file missing from classpath");
try (InputStream in = url.openStream()) { /* preflight read */ }

Try / catch

// Java
try {
  String version = MetadataLoader.getVersion(system);
} catch (IllegalStateException e) {
  // inspect cause (IOException) and log packaging/classpath failure
}

Prevention

When it happens

Trigger: Calling loadSqVersionEol(), loadSqPremiumVersionEol(), loadSqIsLta() or getVersion() when the resource file exists as a URL but its stream cannot be opened/read (corrupted jar, broken classloader, packaging that omitted the file's content).

Common situations: Building SonarQube runtime metadata in a shaded/repackaged jar where internal metadata files were excluded by a packaging filter; custom classloaders failing to expose the resource stream.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at sonar-plugin-api-impl/src/main/java/org/sonar/api/internal/MetadataLoader.java:82

  public static String loadSqPremiumVersionEol(System2 system) {
    return getParamFromFile(system, SQ_VERSION_PREMIUM_EOL_FILE_PATH);
  }

  public static boolean loadSqIsLta(System2 system) {
    return Boolean.parseBoolean(getParamFromFile(system, SQ_IS_LTA_FILE_PATH));
  }

  private static Version getVersion(System2 system, String versionFilePath) {
    return Version.parse(getParamFromFile(system, versionFilePath));
  }

  private static String getParamFromFile(System2 system, String filePath) {
    URL url = system.getResource(filePath);
    try (Scanner scanner = new Scanner(url.openStream(), StandardCharsets.UTF_8)) {
      return scanner.nextLine();
    } catch (IOException e) {
      throw new IllegalStateException(format(CAN_NOT_LOAD_FROM_CLASSPATH, filePath), e);
    }
  }

  public static SonarEdition loadEdition(System2 system) {
    URL url = system.getResource(EDITION_FILE_PATH);
    if (url == null) {
      return SonarEdition.COMMUNITY;
    }
    try (Scanner scanner = new Scanner(url.openStream(), StandardCharsets.UTF_8)) {
      String editionInFile = scanner.nextLine();
      return parseEdition(editionInFile);
    } catch (IOException e) {
      throw new IllegalStateException(format(CAN_NOT_LOAD_FROM_CLASSPATH, EDITION_FILE_PATH), e);
    }
  }

  static SonarEdition parseEdition(String edition) {
    String str = trimToEmpty(edition.toUpperCase(Locale.ENGLISH));

View on GitHub (pinned to 184c821202)