1

I am creating a java 11 spring boot (2.1.3 version) multi module project but I am getting a following exception while running the child module. Everything was working fine when I was running it as a single project ( no multi module).

java.lang.IllegalStateException: Cannot load configuration class: f

etch.App
    at spring.context@5.1.5.RELEASE/org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:413) ~[spring-context-5.1.5.RELEASE.jar:na]
    at spring.context@5.1.5.RELEASE/org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:253) ~[spring-context-5.1.5.RELEASE.jar:na]
    at spring.context@5.1.5.RELEASE/org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:286) ~[spring-context-5.1.5.RELEASE.jar:na]
    at spring.context@5.1.5.RELEASE/org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:130) ~[spring-context-5.1.5.RELEASE.jar:na]
    at spring.context@5.1.5.RELEASE/org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705) ~[spring-context-5.1.5.RELEASE.jar:na]
    at spring.context@5.1.5.RELEASE/org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[spring-context-5.1.5.RELEASE.jar:na]
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) ~[spring-boot-2.1.3.RELEASE.jar:na]
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.1.3.RELEASE.jar:na]
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-2.1.3.RELEASE.jar:na]
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) ~[spring-boot-2.1.3.RELEASE.jar:na]
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) ~[spring-boot-2.1.3.RELEASE.jar:na]
    at legalAppFetchModule/fetch.App.main(App.java:16) ~[classes/:na]
Caused by: java.lang.IllegalStateException: Unable to load cache item
    at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:79) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:134) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:319) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:569) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:416) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at spring.context@5.1.5.RELEASE/org.springframework.context.annotation.ConfigurationClassEnhancer.createClass(ConfigurationClassEnhancer.java:137) ~[spring-context-5.1.5.RELEASE.jar:na]
    at spring.context@5.1.5.RELEASE/org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:109) ~[spring-context-5.1.5.RELEASE.jar:na]
    at spring.context@5.1.5.RELEASE/org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:402) ~[spring-context-5.1.5.RELEASE.jar:na]
    ... 11 common frames omitted
Caused by: java.lang.IllegalAccessError: class fetch.App$$EnhancerBySpringCGLIB$$4423c935 (in module legalAppFetchModule) cannot access class org.springframework.cglib.core.ReflectUtils (in unnamed module @0x50378a4) because module legalAppFetchModule does not read unnamed module @0x50378a4
    at legalAppFetchModule/fetch.App$$EnhancerBySpringCGLIB$$4423c935.CGLIB$STATICHOOK1(<generated>) ~[classes/:na]
    at legalAppFetchModule/fetch.App$$EnhancerBySpringCGLIB$$4423c935.<clinit>(<generated>) ~[classes/:na]
    at java.base/java.lang.Class.forName0(Native Method) ~[na:na]
    at java.base/java.lang.Class.forName(Class.java:398) ~[na:na]
    at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:537) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:363) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:582) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:110) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:108) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
    at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    ... 19 common frames omitted

Please see the parent pom.xml

<?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">
    <modelVersion>4.0.0</modelVersion>
    <!--  lookup parent from repository --> 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> 
    </parent>

    <groupId>com.example</groupId>
    <artifactId>demoMultiModule</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demoMultiModule</name>
    <description>Demo project for Spring Boot</description>
    <packaging>pom</packaging>
    <properties>
        <java.version>11</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


    <modules>
        <module>fetch</module>
    </modules>
</project>

Please see the child pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.example</groupId>
    <artifactId>demoMultiModule</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
    <artifactId>fetch</artifactId>

  <name>fetch</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </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>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

I could see a related spring issue (https://github.com/spring-projects/spring-framework/issues/20414) and so question (Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1). According to them it must have fixed in spring5.1+ and I am using Spring-web.5.1.5. Therefore it should not have any issue.

For sake of completeness, please see my module path and module-info.java file of child module enter image description here

module-info.java

open module legalAppFetchModule {
    requires spring.boot.autoconfigure;
    requires spring.boot;
    requires spring.web;
}

Can somebody please throw some light

nantitv
  • 3,539
  • 4
  • 38
  • 61

3 Answers3

0

After adding spring-core and spring-beans to the module path , it does not complains any more still the server is not running fully but I think it may be due to something else which I have to look further. But as far the posted issue is concerned, it is solved

enter image description here

nantitv
  • 3,539
  • 4
  • 38
  • 61
  • Now it fails with Caused by: java.lang.NoClassDefFoundError: java/lang/instrument/ClassFileTransformer at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na] at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3167) ~.... – nantitv Apr 19 '19 at 06:12
0

Add spring.core and spring.beans to your module dependencies (which you have already done).

Also, add this to the VM arguments of the arguments tab of the run configuration of your application:

--add-modules=ALL-SYSTEM

You could also set this as a default option under Window > Preferences > Java > Installed JREs Select your JRE/JDK and edit. Add the option above under Default VM Arguments

ddamw
  • 16
  • 1
  • adding --add-modules=ALL-SYSTEM worked. I went further and added just the instrument module which contained the java.lang.instrument classes. So basically I only added --add-modules=java.instrument. in the vm args – nantitv Apr 21 '19 at 18:35
  • I got the module info by running following command . java --describe-module java.instrument java.instrument@11.0.2 exports java.lang.instrument requires java.base mandated qualified exports sun.instrument to java.base – nantitv Apr 21 '19 at 18:36
  • Do you know why this was not an issue with non-multi module java 11 spring boot projects – nantitv Apr 21 '19 at 18:38
  • Sadly, I don't know. I am new to the module system and found the solution through trial and error. As your solution with spring.beans helped me I wanted to return the favor and make sure others don't get stuck either. – ddamw Apr 23 '19 at 07:18
0

Probably your problem is related with incorrect modules configuration in your package-info.java file.

Caused by: java.lang.IllegalAccessError: class fetch.App$$EnhancerBySpringCGLIB$$4423c935 (in module legalAppFetchModule) cannot access class org.springframework.cglib.core.ReflectUtils (in unnamed module @0x50378a4) because module legalAppFetchModule does not read unnamed module @0x50378a4

In module legalAppFetchModule, please add in module-info.java:

    requires spring.context;
    requires spring.core;
    requires spring.beans;

If you are using Hibernate than probably you need also add to above command:

requires org.hibernate.orm.core;
sobczak.dev
  • 973
  • 7
  • 11