How to pass jdk 21 version in script

HI Team
My application is developed on Java Development Kit (JDK) 11, but I am trying to pass Java Development Kit (JDK) 21 for Sonar.

Please find the below details.
mvn
-Dsonar.host.url=“$SONAR_HOST_URL”
-Dsonar.login=“$SONAR_PASSWORD”
-Dsonar.projectName=“$SONAR_PROJECT_NAME”
-Dsonar.projectKey=“$SONAR_PROJECT_KEY”
-Dsonar.branch.name=“$BRANCH_NAME”
-Dsonar.scm.disabled=true
-Dsonar.java.binaries=“$SONAR_JAVA_BINARIES”
-Dsonar.java.source=21
sonar:sonar

for jdk version i am using -Dsonar.java.source=21
Please suggest.

Thanks
Mitesh

Hey there!

  • What version of SonarQube are you using?
  • What are you trying to accomplish by passing JDK 21 to Sonar for your Java 11 project?
  • What was the output of what you already tried? What indicated to you that it didn’t work?

Error:
build 19-Feb-2025 09:03:26 [ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:4.0.0.4121:sonar (default-cli) on project X: 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

1 ) I want to use JDK 21 and i am using script.

Thanks. What I think you want to do is make sure that JAVA_HOME is pointing to a Java 17 installation or higher. See the docs here.

Adjusting sonar.java.source is having no affect on this error message.

With all that said, this version of the SonarScanner for Maven should support JRE Auto-provisioning as long as you’re connecting to a version of SonarQube v10.6 or higher. In this case you shouldn’t have to do anything to have a successful scan.

What version of SonarQube are you using? This information is available in the footer of your SonarQube instance.

EDIT: In fact, it’s v5.0 of the Scanner for Maven which supports this.

Thanks for reply.
sonar version : v10.6 (92116)

Then I suggest you bump up your version of the Scanner for Maven.

And, v10.6 is no longer a supported version of SonarQube. I suggest you upgrade to the latest version. However, your existing setup should support JRE auto-provisioning like I mentioned.

Ok, Thanks for your help. Tc bye