Java parse error "source level 1.8 or above" although project uses 11

I’m using SonarCloud on a few of my GitHub repos. Today I realized that there are some errors in the GitHub Actions build log:

[ERROR] Unable to parse source file : 'src/main/java/com/github/beatngu13/knapsackproblem/so/SingleObjectiveProblem.java'
[ERROR] Parse error at line 19 column 51: References to interface static methods are allowed only at source level 1.8 or above
[ERROR] Unable to parse source file : 'src/main/java/com/github/beatngu13/knapsackproblem/base/KnapsackFactory.java'
[ERROR] Parse error at line 49 column 12: Lambda expressions are allowed only at source level 1.8 or above
[ERROR] Unable to parse source file : 'src/main/java/com/github/beatngu13/knapsackproblem/mo/ga/KnapsackChromosome.java'
[ERROR] Parse error at line 36 column 9: Method references are allowed only at source level 1.8 or above
...

The project itself uses Maven, where I set the Java version via the compiler plugin (see pom.xml). Sonar is triggered via Maven sonar:sonar as well (see build config).

Is there a configuration I’m missing that tells Sonar to use the Java version declared in the pom.xml, or how can I fix this?

1 Like

The scanner for Maven reads the source and the target versions as declared by the maven compiler plugin. The --release is new to Java 9 and I don’t think the scanner supports it.
I’d try setting the properties maven.compiler.source and maven.compiler.target. If you want to set it at the scanner directly, you can use sonar.java.source and sonar.java.target.

1 Like

Thanks @dmeneses, this removed the warnings!

Should this be reported as a bug? Java 11 is officially supported, so I assume users expect Sonar to automatically obtain the Java version from the pom.xml?

1 Like

I agree that we should support it, I’ve created a ticket: https://jira.sonarsource.com/browse/MSONAR-174

2 Likes

The issue seems to be fixed, but I still experience the aforementioned issues when I remove the source/target properties:

I assume this is because a new version of the SonarScanner (for Maven) hasn’t been released yet?

That’s right. We don’t have a date for the release yet.

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