Jenkins and Sonar - Error during SonarScanner execution

Hi Team,

We are trying to run the Sonar Scans for our JAVA project from our Jenkins job to SonarQube integrations. Below are the details of versions we are using in this job.

Jenkins Version: Jenkins 2.263.3
Java Version: java version “1.8.0_162”

SonarQube Version: Community Edition Version 9.4 (build 54424)
Java in SonarQube Server: openjdk version “11.0.13”

We are getting below errors during the SonarScanner execution. Can someone suggest what could be the cause of this?

ERROR: Error during SonarScanner execution
java.lang.UnsupportedClassVersionError: org/sonar/batch/bootstrapper/EnvironmentInformation has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)

Thank you,
Laxmi

Hi Laxmi,

That error means you’re using an incompatible version of Java. Per the docs, Java 8 isn’t supported. You’ll need to upgrade to Java 11 or 17.

 
HTH,
Ann

Thank you Ann…

Ann,
One more question…
This means, we need to have JAVA 11 or 17 on Jenkins and need to compile our code with these versions and also SonarScanners need these version?
Or, Irrespective of in which Java the code is compiled (Like Build can go using Java 1.8), we just need SonarScanner to use Java 11 and above? Can you please give some inputs on this. Any more information would be greatly appreceiated.

Thank,
Laxmi.

Hi Laxmi,

You can compile with, or to, whatever version you like. But you must analyze with a supported version.

 
HTH,
Ann

Hi Laxmi,

the sonarqube analysis itself has to run with Java 11 that’s all.
Guess you’re using Maven as build tool and Jenkins pipelines !?

i.e. some snippet of a pipeline using the SonarScanner for Maven

nodejs(configId: genericNpmSettings(), nodeJSInstallationName: 'nodejs-16') {
  withSonarQubeEnv(sonarenv) {
    withMaven(jdk: 'java-11-openjdk', maven: "${p.mvnVersion}", globalMavenSettingsConfig: genericMvnSettings()) {
      genericSh("mvn ${sonarDebug ? '-X' : ''} ${p.mvnPomPath ? "-f \"$p.mvnPomPath\"" : ''} ${isDMZDE ? mvnDMZDEparams : ''} sonar:sonar ${sqProjectVersion ? "-Dsonar.projectVersion=$sqProjectVersion" : ''} -Dsonar.branch.name=$BRANCH_NAME")
    }
  }
}
println 'Sonarqube Analysis \u2714'

println '\u27B9 start Sonarqube Quality Gate ..'
timeout(time:10 , unit: 'MINUTES') {
  def qg = waitForQualityGate()
  if (qg.status != 'OK') {
    error "Pipeline aborted due to quality gate failure: ${qg.status}"
  }
}
println 'Sonarqube Quality Gate \u2714'

Note you will need nodejs to analyze Javascript, Typescript and CSS hence the
surrounding nodejs(...) { ... }

Gilbert

1 Like

Hi Glibert - Thank you

Hi Gilbert,
If we have a Gradle project and we need to explicitly mention the JAVA 11 or JAVA 17 for the Sonar Scan, what would be the snippet to use in JenkinsFile?
Because our code is compiling with JAVA 1.8, but we need to call the Sonar scan with JAVA 11 or 17 to make it compatible…

Example you have given for maven is below:
withMaven(jdk: ‘java-11-openjdk’, maven: “${p.mvnVersion}”, globalMavenSettingsConfig: genericMvnSettings())

Thank you,
Laxmi.

Hi Laxmi,

you may use the Sonar CLI scanner with Gradle like that, i.e.

stage('\u2778 Sonarqube Analysis') {

  env.JAVA_HOME="${tool 'java-11-openjdk'}"
  env.PATH="${env.JAVA_HOME}/bin:${env.PATH}"
  scannerHome = tool 'sonar-scanner-4.7';

 nodejs(configId: ... , nodeJSInstallationName: 'nodejs-16') {
   withSonarQubeEnv('yourJenkinsSonarConfigName') {
     sh "${scannerHome}/bin/sonar-scanner -X"
    }
  }
}

Cross compile = using Java11 and compile for Java8 is also possible.

Gilbert

1 Like

Thank you Gilbert, we will try it out.

Thank you,
Laxmi.