{"record":{"id":"107d328dd0d7334f","repo":"SonarSource/sonarqube","slug":"format-can-not-load-from-classpath-filepath","errorCode":null,"errorMessage":"format(CAN_NOT_LOAD_FROM_CLASSPATH, filePath)","messagePattern":"format\\(CAN_NOT_LOAD_FROM_CLASSPATH, filePath\\)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"sonar-plugin-api-impl/src/main/java/org/sonar/api/internal/MetadataLoader.java","lineNumber":82,"sourceCode":"\n  public static String loadSqPremiumVersionEol(System2 system) {\n    return getParamFromFile(system, SQ_VERSION_PREMIUM_EOL_FILE_PATH);\n  }\n\n  public static boolean loadSqIsLta(System2 system) {\n    return Boolean.parseBoolean(getParamFromFile(system, SQ_IS_LTA_FILE_PATH));\n  }\n\n  private static Version getVersion(System2 system, String versionFilePath) {\n    return Version.parse(getParamFromFile(system, versionFilePath));\n  }\n\n  private static String getParamFromFile(System2 system, String filePath) {\n    URL url = system.getResource(filePath);\n    try (Scanner scanner = new Scanner(url.openStream(), StandardCharsets.UTF_8)) {\n      return scanner.nextLine();\n    } catch (IOException e) {\n      throw new IllegalStateException(format(CAN_NOT_LOAD_FROM_CLASSPATH, filePath), e);\n    }\n  }\n\n  public static SonarEdition loadEdition(System2 system) {\n    URL url = system.getResource(EDITION_FILE_PATH);\n    if (url == null) {\n      return SonarEdition.COMMUNITY;\n    }\n    try (Scanner scanner = new Scanner(url.openStream(), StandardCharsets.UTF_8)) {\n      String editionInFile = scanner.nextLine();\n      return parseEdition(editionInFile);\n    } catch (IOException e) {\n      throw new IllegalStateException(format(CAN_NOT_LOAD_FROM_CLASSPATH, EDITION_FILE_PATH), e);\n    }\n  }\n\n  static SonarEdition parseEdition(String edition) {\n    String str = trimToEmpty(edition.toUpperCase(Locale.ENGLISH));","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/sonar-plugin-api-impl/src/main/java/org/sonar/api/internal/MetadataLoader.java#L64-L100","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Verify the resource file exists in the jar/classpath and is not stripped by maven-shade-plugin/assembly excludes.","Check the IOException cause in the stack trace to identify the underlying stream failure.","Ensure the plugin runs on a standard SonarQube runtime rather than a repackaged environment."],"exampleFix":"// packaging fix (maven-shade-plugin)\n// before\n<filters>\n  <filter><artifact>*:*</artifact>\n    <excludes><exclude>sonar-api-version.txt</exclude></excludes>\n  </filter>\n</filters>\n// after: remove the exclude so the metadata file ships in the jar","handlingStrategy":"try-catch","validationCode":"// Java\nURL url = system.getResource(\"/sonar-api-version.txt\");\nif (url == null) throw new IllegalStateException(\"Metadata file missing from classpath\");\ntry (InputStream in = url.openStream()) { /* preflight read */ }","typeGuard":null,"tryCatchPattern":"// Java\ntry {\n  String version = MetadataLoader.getVersion(system);\n} catch (IllegalStateException e) {\n  // inspect cause (IOException) and log packaging/classpath failure\n}","preventionTips":["Verify metadata files (sonar-api-version.txt etc.) survive shading/repackaging with unzip -l.","Do not exclude internal metadata resources in maven-shade-plugin/assembly filters.","Smoke-test plugin startup in the real SonarQube runtime."],"tags":["classpath","metadata","sonarqube-plugin"],"backgroundTag":"resource-not-found","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}