Error during analysis

Hi,
My java (ver. 1.8) project builds fine (mvn clean package), but when I do SonarQube analysis (mvn sonar:sonar), I get an error:

[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.8.0.2131:sonar (default-cli) on project manipulator: Execution default-cli of goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.8.0.2131:sonar failed: An API incompatibility was encountered while executing org.sonarsource.scanner.maven:sonar-maven-plugin:3.8.0.2131:sonar: java.lang.UnsupportedClassVersionError: javafx/stage/Stage 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

Why?

Marek

This really has nothing to do with SonarQube. The “52.0” and “55.0” numbers refer to versions of the JDK. The former refers to Java 8, and the latter Java 11. You apparently have class files that were compiled with Java 11, but you’re attempting to run with Java 8. Java 8 cannot use class files compiled with a newer JDK. You typically control this in Maven with the “maven-compiler-plugin”, setting the “source” and “target” properties, so that all of your code is compiled with expected versions of the JDK.

These are the essential tags in my pom file:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <mainClass>com.company.manipulator.MainApp</mainClass>        
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>                  
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.3</version>
            <configuration>
                <mainClass>com.company.manipulator.MainApp</mainClass>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.company.manipulator.MainApp</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.company.manipulator.MainApp</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>2.4.1</version>
            <configuration>
             <skip>true</skip>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>13</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>13</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.14.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.14.0</version>
    </dependency>

    <dependency>
        <groupId>org.snmp4j</groupId>
        <artifactId>snmp4j</artifactId>
        <version>2.8.6</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-junit-jupiter</artifactId>
        <version>2.23.4</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>3.6.28</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
        <version>2.12.1</version>
    </dependency>
    
    <dependency>
      <groupId>org.junit.platform</groupId>
      <artifactId>junit-platform-launcher</artifactId>
      <version>1.8.0-M1</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
       <groupId>org.openjfx</groupId>
       <artifactId>javafx-swing</artifactId>
       <version>16-ea+7</version>
    </dependency>
                    
    <dependency>
        <groupId>org.sonarsource.scanner.maven</groupId>
        <artifactId>sonar-maven-plugin</artifactId>
        <version>3.0</version>
    </dependency>          
</dependencies>
</project>

So where can class files compiled in Java 11 come from?
Let me remind, that the project builds correctly using a command “mvn clean package”.

We would have no idea how you are getting classes compiled with Java 11.

I suppose none of the goals executed by “clean package” have Java version dependencies, which is why nothing fails with that command line.

Are you certain that the version of Java in the path when you run any “mvn” command line is Java 8, and not Java 11?

The plugin javafx-maven-plugin was compiled for Java 11 so you need to run Maven with Java 11.

With Java 11, you should use <release>1.8</release> instead of <source>1.8</source><target>1.8</target> in the configuration of maven-compiler-plugin to indicate to Java compiler that source are 1.8 and that you want the generate byte code to be compatible with 1.8 (see JEP 247).

Also you probably don’t want to declare sonar-maven-plugin as a dependency as it is a Maven plugin.