Home > OS >  Spring Boot Java modules = Unsupported class file major version
Spring Boot Java modules = Unsupported class file major version

Time:10-07

I have weird problem that mvn clean test fails only when I try to add module-info.java to my project using Spring Boot. Project structure is very simple

    │   pom.xml
    │
    ├───.idea
    │       .gitignore
    │       compiler.xml
    │       encodings.xml
    │       jarRepositories.xml
    │       misc.xml
    │       workspace.xml
    │
    └───src
        ├───main
        │   ├───java
        │   │   │   module-info.java
        │   │   │
        │   │   └───com
        │   │       └───test
        │   └───resources
        └───test
            ├───java
            │   └───com
            │       └───test
            │               SomeTest.java
            │
            └───resources

SomeTest.java is very simple


import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SomeTest {
    @Test
    public void test() {
        var isTrue = true;
        Assertions.assertTrue(isTrue);
    }
}

module-info.java also:

open module com.test {
}

pom.xml file is taken from sprint initalizr

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>SampleTest</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

And now the weird thing is that when module-info.java file is present then mvn clean test fails Unsupported class file major version 61

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project SampleTest: 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]

but when I delete module-info.java then everything goes without error. Any ideas how can I achieve java modules with spring boot? :)

CodePudding user response:

You have to overwrite the used maven-surefire-plugin version because the version defined by spring boot parent is too old.

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
            </plugin>
        </plugins>
    </pluginManagement>
...
  • Related