SonarSource/sonarqube · error · IllegalStateException

Can't read svg template

Error message

Can't read svg template '%s'

What it means

SvgGenerator.readTemplate loads a badge SVG template from the classpath via clazz.getResource(template); if reading the resource throws an IOException it wraps it in this IllegalStateException. This is an internal deployment/classpath problem — the template resource should always be bundled inside the SonarQube webapp.

Solutions

  1. Re-install SonarQube from an official, unmodified distribution
  2. Verify the SVG template files exist inside the server JARs (unzip and check)
  3. Ensure no custom plugin/shading step strips resources from the webapp jars
  4. Clear any stale extracted artifacts and restart the server

Example fix

// before (corrupted install)
sonarqube/lib/sonar-webapi-webapi-*.jar  (missing measure.svg template)
// after
re-download and deploy the official SonarQube distribution
Defensive patterns

Strategy: try-catch

Validate before calling

// Deployment self-check: the badge SVG templates must be on the classpath
URL tpl = SvgGenerator.class.getResource("/svg/measure.svg");
if (tpl == null) throw new IllegalStateException("SVG badge templates missing from classpath — reinstall SonarQube");

Try / catch

try {
  String svg = SvgGenerator.readTemplate(template, clazz);
} catch (IllegalStateException e) {
  if (e.getMessage().startsWith("Can't read svg template")) {
    log.error("Badge template unreadable — verify server installation integrity", e);
    return fallbackBadgeSvg();
  } else { throw e; }
}

Prevention

When it happens

Trigger: Rendering a quality gate or measure badge when the SVG template resource cannot be read from the jar/classpath (missing/corrupt sonar-webapi jar, broken deployment artifact).

Common situations: Partially extracted or corrupted SonarQube installation; a custom classloader/packaging that excludes .svg resources; running a repackaged/hacked WAR.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


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

Appendix: source

Thrown at server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/SvgGenerator.java:173

  }

  public String generateQualityGate(Metric.Level level) {
    return qualityGateTemplates.get(level);
  }

  public String generateError(String error) {
    Map<String, String> values = ImmutableMap.of(
      PARAMETER_TOTAL_WIDTH, valueOf(MARGIN + computeWidth(error) + MARGIN),
      PARAMETER_LABEL, error);
    StringSubstitutor strSubstitutor = new StringSubstitutor(values);
    return strSubstitutor.replace(errorTemplate);
  }

  public static String readTemplate(String template, Class<?> clazz) {
    try {
      return IOUtils.toString(clazz.getResource(template), UTF_8);
    } catch (IOException e) {
      throw new IllegalStateException(String.format("Can't read svg template '%s'", template), e);
    }
  }

  private static int computeWidth(String text) {
    return text.chars()
      .mapToObj(i -> (char) i)
      .mapToInt(c -> {
        Integer length = CHAR_LENGTH.get(c);
        checkState(length != null, "Invalid character '%s'", c);
        return length;
      })
      .sum();
  }

  private String readTemplate(String template) {
    return readTemplate(template, getClass());
  }

View on GitHub (pinned to 184c821202)