14

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
  • 676
  • 3
  • 19
  • 1
    "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
  • This has been fixed in `org.apache.maven.plugins:maven-surefire-plugin` version `3.0.0-M5` – lealceldeiro Jul 13 '22 at 06:41

1 Answers1

10

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 Genese
  • 1,639
  • 2
  • 21
  • 37
  • Interesting. It works. Why? How does this work behind scenes? – lealceldeiro Jul 12 '22 at 20:24
  • 1
    It's been a while since I wrote this answer, but I had to enable verbose logs in maven when it failed and followed the stack trace to ASM sources on GitHub to understand the under-the-hood issue. While I don't remember the specifics, I suppose you could follow the same lead. By the way, it's a design choice of Maven vs. that of Gradle. – Salathiel Genese Jul 13 '22 at 01:08
  • Sounds unlikely to be a design choice of Maven. It seems rather that some test framework is running it's own JVM instance to run the tests. – Ondra Žižka Sep 06 '22 at 22:47
  • You got me to search it again, @OndraŽižka. So here is it in asm sources - https://gitlab.ow2.org/asm/asm/-/blob/master/asm/src/main/java/org/objectweb/asm/ClassReader.java#L199 You should check how gradle tackles the same issue - it comes down to choices but none it outstandingly better. – Salathiel Genese Sep 08 '22 at 17:58
  • 1
    Well but then it is a design choice of ASM, not Maven :) Maven Surefire plugin uses ASM which happens to opt to fail on untested versions of bytecode. Still, thanks for finding this out, it saved me a lot of time :) – Ondra Žižka Sep 09 '22 at 07:18