Sonar scanner - Java Runtime error, class version mismatch

Hi all,

I’m trying to run sonar-scanner in AWS CodeBuild for a CodeCommit repository mainly containing Python code.
I’m having issues with the version of the Java Runtime.
Here is the last command that I tried:

sonar-scanner -Dsonar.java.jdkHome=/usr/lib/jvm/java-11-amazon-corretto -Dsonar.organization=$Organization -Dsonar.projectKey=$Project -Dsonar.sources=. -Dsonar.host.url=$HOST -Dsonar.login=$LOGIN

And here is the error:

INFO: Scanner configuration file: /codebuild/output/src025386270/src/sonar-scanner-3.3.0.1492-linux/conf/sonar-scanner.properties
967	INFO: Project root configuration file: NONE
968	INFO: SonarQube Scanner 3.3.0.1492
969	INFO: Java 1.8.0_121 Oracle Corporation (64-bit)
970	INFO: Linux 4.14.281-212.502.amzn2.x86_64 amd64
971	INFO: User cache: /root/.sonar/cache
972	INFO: SonarQube server 8.0.0.31618
973	INFO: Default locale: "en_US", source code encoding: "UTF-8" (analysis is platform dependent)
974	INFO: ------------------------------------------------------------------------
975	INFO: EXECUTION FAILURE
976	INFO: ------------------------------------------------------------------------
977	INFO: Total time: 1.739s
978	INFO: Final Memory: 5M/164M
979	INFO: ------------------------------------------------------------------------
980	ERROR: Error during SonarQube Scanner execution
981	ERROR: 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
982	ERROR: 

It looks like it’s using Java 8 instead of Java 11, even though I specified in the command to use Java 11. Also, JAVA_HOME and PATH looks correctly pointing to the right paths:

echo $JAVA_HOME
/usr/lib/jvm/java-11-amazon-corretto

echo $PATH
/usr/lib/jvm/java-11-amazon-corretto/bin:/usr/local/bin/sbt/bin:/root/.phpenv/shims:/root/.phpenv/bin:/root/.goenv/shims:/root/.goenv/bin:/go/bin:/root/.phpenv/shims:/root/.phpenv/bin:/root/.pyenv/shims:/root/.pyenv/bin:/root/.rbenv/shims:/usr/local/rbenv/bin:/usr/local/rbenv/shims:/root/.dotnet/:/root/.dotnet/tools/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/tools:/codebuild/user/bin:./sonar-scanner-3.3.0.1492-linux/bin/

Both version of Java are installed in the machine:

ls /usr/lib/jvm
926	java-1.8.0-amazon-corretto
927	java-11-amazon-corretto

Do you have any idea how to fix it?
Thanks a lot!

Hi,

sonar.java.jdkHome isn’t doing what you think it is. From the docs:

…set the property sonar.java.jdkHome manually (see below). By doing this you’ll specify which JDK classes the analyzer must refer to during the analysis

It tells the analyzer which version of Java the project was compiled with, not which version of Java the analyzer should have started itself with (you see the bootstrap problem there?).

You might try running java -version. I suspect it will point to the Java 8 install.

 
HTH,
Ann

1 Like

Hi,

Thank you for your answer!
Ok, I did not know that about the property sonar.java.jdkHome, it is not useful then.

However, java -version returns:

900  java -version
901	openjdk version "11.0.15" 2022-04-19 LTS
902	OpenJDK Runtime Environment Corretto-11.0.15.9.1 (build 11.0.15+9-LTS)
903	OpenJDK 64-Bit Server VM Corretto-11.0.15.9.1 (build 11.0.15+9-LTS, mixed mode)

Do you have any idea why it picks another version after?

Hi,

Okay, I’ve just reviewed my own sonar-scanner installation & I’m reminded that it ships with a JRE. So then I went back to your OP & saw this:

That’s a pretty old version. The current version is 4.7. Can you upgrade and try again?

 
Ann

2 Likes

Hi, thanks again for checking.
You are totally right. After updating it to the latest version it works as expected.

Thanks a lot!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.