2

I am trying to create a project in IntelliJ using Maven, but when running mvn install or mvn test in order to run the simple JUnit tests I wrote, it fails with the following error:

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project Idlearn: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test failed: Unsupported class file major version 61 -> [Help 1]

My pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>mimuw</groupId>
    <artifactId>Idlearn</artifactId>
    <version>1</version>
    <name>Idlearn</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- for platform independent encoding-->
        <junit.version>5.8.2</junit.version> <!-- the latest JUnit version -->
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${junit.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

I feel like I have tried every possible solution on the web, but to no avail.

The problem originally was that my tests wouldn't run. The command mvn test would output that it was running SomeClassTest however not run any actual tests that it contained (all created just like in many tutorials I found on-line). It seems like I was missing maven-surefire and here I am now.

Maurycyt
  • 410
  • 1
  • 12
  • "Unsupported class file major version" always means that some class is compiled against some Java version that is higher than the Java version you're using to run. Version 61 means Java 17 (see https://stackoverflow.com/a/11432195/1180351), which matches with your compiler version in `pom.xml`. Make sure you're running the tests using Java 17 as well. See what `java -version` prints, and what `echo %JAVA_HOME%` (Windows) or `echo $JAVA_HOME` (Linux) prints. – Rob Spoor Mar 16 '22 at 11:15
  • `java -version` prints `openjdk version "17.0.2" 2022-01-18 (...)` as expected. `echo $JAVA_HOME` prints an empty line... which is weird because I have `export JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"` in `~/.profile`. – Maurycyt Mar 16 '22 at 11:24
  • Ok I reset the system and now `echo $JAVA_HOME` correctly prints the expected line, but the problem persists. – Maurycyt Mar 16 '22 at 11:26
  • From what I understand the problem in the post linked by you was that the runtime environment was older than the jdk that the code was developed in. However I don't think I have anything but Java 17.0.2 on my machine and I cannot find a trace of any wrong jre being used, although I am not knowledgable in this stuff so my efforts are probably quite ineffective. – Maurycyt Mar 16 '22 at 11:53
  • What does `mvn --version` show? Especially the Java version part is interesting. – Rob Spoor Mar 16 '22 at 11:56
  • `Apache Maven 3.8.5 (3599d3414f046de2324203b78ddcf9b5e4388aa0) Maven home: /opt/apache-maven-3.8.5 Java version: 17.0.2, vendor: Private Build, runtime: /usr/lib/jvm/java-17-openjdk-amd64` – Maurycyt Mar 16 '22 at 12:17
  • So everything indicates that you're using Java 17, but you still get the dreaded version mismatch error. I'm afraid to say I don't have any idea what's going on then. You can try to use the latest milestone version of the surefire plugin (3.0.0-M5), but I doubt that will solve anything. – Rob Spoor Mar 16 '22 at 12:32

1 Answers1

0

This is what worked for me using Java 17.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.ow2.asm</groupId>
            <artifactId>asm</artifactId>
            <version>9.1</version>
        </dependency>
    </dependencies>
</plugin>

And when upgrading to Java 18 this morning, I had to upgrade to asm v9.3 as well.

I hope it helps.

Salathiel Genèse
  • 1,246
  • 2
  • 18
  • 32