Troubleshooting Maven Surefire Test Failed: Unsupported Class File Major Version 61

Published on 2023.11.29

Introduction

When working with Maven and running tests using the Surefire plugin, you may encounter an error message like:

UnsupportedClassVersionError: Unsupported major.minor version 61.0

This error occurs when you are trying to run tests compiled with a higher Java version than the one supported by the JVM executing the tests. In this article, we will explore the steps to troubleshoot and resolve this issue.

Step 1: Check Java Version

The first step is to check the version of Java installed on your system. Open a terminal or command prompt and run the following command:

java -version

Ensure that the Java version is compatible with the Java version used to compile the tests. If the version is lower, you need to update your Java installation.

Step 2: Update Java Version

If the Java version is lower than the one used to compile the tests, you need to update your Java installation. Here are the steps to update Java:

  1. Visit the Oracle Java website or the OpenJDK website and download the latest Java Development Kit (JDK) for your operating system.
  2. Run the installer and follow the instructions to install the updated JDK.
  3. After the installation is complete, open a terminal or command prompt and run java -version again to verify that the Java version has been updated.

Step 3: Update Maven Configuration

Sometimes, Maven may be configured to use a specific Java version or use the system's default Java version. To ensure Maven uses the updated Java version, you need to update the Maven configuration:

  1. Open the pom.xml file of your project in a text editor or an Integrated Development Environment (IDE).
  2. Locate the <build> section within the pom.xml file.
  3. Add or update the <plugins> section within the <build> section, including the following configuration:
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
      <source>YOUR_UPDATED_JAVA_VERSION</source>
      <target>YOUR_UPDATED_JAVA_VERSION</target>
    </configuration>
  </plugin>
</plugins>

Replace YOUR_UPDATED_JAVA_VERSION with the Java version you installed in Step 2.

Step 4: Rebuild and Run Tests

After updating the Java version and Maven configuration, you need to rebuild your project and rerun the tests. Run the following command in your project directory:

mvn clean test

This command will rebuild the project and run the tests using the updated Java version.

Conclusion

By following these steps, you should be able to troubleshoot and resolve the UnsupportedClassVersionError caused by an unsupported class file major version. Ensuring that the Java version is up to date and configuring Maven to use the correct Java version will help you run the tests successfully.