How to Resolve SonarQube Maven Plugin Java Version Compatibility Error?

I am encountering an issue when trying to run the SonarQube analysis on my project using the SonarQube Maven plugin. The error message indicates that there is an API incompatibility due to the Java Runtime version. Below is the detailed error message:
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:4.0.0.4121:sonar (default-cli) on project XplTrack: Execution default-cli of goal org.sonarsource.scanner.maven:sonar-maven-plugin:4.0.0.4121:sonar failed: An API incompatibility was encountered while executing org.sonarsource.scanner.maven:sonar-maven-plugin:4.0.0.4121:sonar: java.lang.UnsupportedClassVersionError: org/sonar/batch/bootstrapper/EnvironmentInformation has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0

[ERROR] -----------------------------------------------------

[ERROR] realm = plugin>org.codehaus.mojo:sonar-maven-plugin:4.0.0.4121

[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy

[ERROR] urls[0] = file:/C:/Users/ZAKARIA/.m2/repository/org/sonarsource/scanner/maven/sonar-maven-plugin/4.0.0.4121/sonar-maven-plugin-4.0.0.4121.jar

[ERROR] urls[1] = file:/C:/Users/ZAKARIA/.m2/repository/org/sonatype/plexus/plexus-sec-dispatcher/1.4/plexus-sec-dispatcher-1.4.jar

[ERROR] urls[2] = file:/C:/Users/ZAKARIA/.m2/repository/org/codehaus/plexus/plexus-utils/1.5.5/plexus-utils-1.5.5.jar

[ERROR] urls[3] = file:/C:/Users/ZAKARIA/.m2/repository/org/sonatype/plexus/plexus-cipher/1.4/plexus-cipher-1.4.jar

[ERROR] urls[4] = file:/C:/Users/ZAKARIA/.m2/repository/org/sonarsource/scanner/api/sonar-scanner-api/2.16.3.1081/sonar-scanner-api-2.16.3.1081.jar

[ERROR] urls[5] = file:/C:/Users/ZAKARIA/.m2/repository/commons-lang/commons-lang/2.6/commons-lang-2.6.jar

[ERROR] Number of foreign imports: 1

[ERROR] import: Entry[import from realm ClassRealm[maven.api, parent: null]]

[ERROR] -----------------------------------------------------

[ERROR]

[ERROR] -> [Help 1]

[ERROR]

[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.

[ERROR] Re-run Maven using the -X switch to enable full debug logging.

[ERROR]

[ERROR] For more information about the errors and possible solutions, please read the following articles:

[ERROR] [Help 1] PluginContainerException - Apache Maven - Apache Software Foundation

Additional Context

  • SonarQube Version: 10.6
  • Maven Version: 3.8.1
  • Java Version: 11.0.25 (LTS)

pom.xml Configuration

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.8.1</version>

<configuration>

<source>11</source>

<target>11</target>

<compilerArgs>

<arg>-parameters</arg>

</compilerArgs>

</configuration>

</plugin>

Command Run

mvn clean verify sonar:sonar \

-Dsonar.projectKey=test-maven \

-Dsonar.projectName='test-maven' \

-Dsonar.host.url=<sonarqube_url> \

-Dsonar.token=<token>

How can I resolve this error and successfully run the SonarQube analysis?

Hey there.

Java 17 is the required JRE to run the scanner in SonarQube v10.4+.

This means you can do a few things:

  • Run your entire build using Java 17, still targeting Java 11 for your build (as you’ve done)
  • Run your build with Java 11 and then switch to Java 17 before running the sonar task (docs)
# Maven build
mvn verify ...
export JAVA_HOME=/path/to/java-17
mvn sonar:sonar ...
  • Upgrade to v5.0 of the SonarScanner for Maven (org.sonarsource.scanner.maven:sonar-maven-plugin), which supports JRE auto-provisioning – meaning you can use Java 11 and Java 17 will be downloaded from SonarQube Cloud and executed during analysis. This should be the least disruptive option!

Hello Colin ,

Thanks for the detailed blog…

  • If we change the java version only during sonar scan will the result may get vary as per java 17 standards or it will be same with any java version.

Thanks :slight_smile:

The results should be the same, however if something goes wrong you can force the Java version as documented here.