Spring Boot 2.6.3 and Java Modules

Viewed 576

I'm trying to build a spring boot (2.6.3) application with java modules (java 17) and gradle(7.4).

right now my folder structure looks like this:

    |- build.gradle.kts
    |- settings.gradle.kts
    |- src
       |- main
           |- java
               |- com.my.package
                   |- Application.java
               |- module-info.java

my module:

    module com.my.package {
        requires spring.boot;
        requires spring.boot.autoconfigure;
        requires spring.boot.actuator.autoconfigure;
        
        opens com.my.package;
    }

The application class:

    @SpringBootApplication
    public class Application {

        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

I already found out that I had to use opens with my package instead of just using exports. If I where to use exports I would get this error:

java.lang.IllegalAccessException-->module com.my.package does not open com.my.package to unnamed module @1a482e36

Now when I run this with the opens I'll get this error here:

java.lang.IllegalAccessError-->superinterface check failed: class com.my.package.Application$$EnhancerBySpringCGLIB$$917dba7a (in module com.my.package) cannot access class org.springframework.context.annotation.ConfigurationClassEnhancer$EnhancedConfiguration (in unnamed module @0x1623b78d) because module com.my.package does not read unnamed module @0x1623b78d

I've read things about using the -classpath JVM argument. But I am not quite certain which path to put there. And to be frank I am trying to find a solution that works without any arguments that need to be passed. So that the experience is similar to spring without (java)modules.

It'd be great if someone could help me to understand what I am doing wrong here. I'm obviously new to java modules and trying to wrap my head around things.

Thanks in advance!

Cheers, Daniel

1 Answers

As dan1st was pointing out --add-reads com.my.package=ALL-UNNAMED did the trick. A more verbose explanation can be found in the question dan1st was sharing I didn't even think of the option that it could be IDE related. (I am using IntelliJ)

Related