Home > Back-end >  Intellij/Maven: Error creating bean when running mvn install
Intellij/Maven: Error creating bean when running mvn install

Time:01-31

When trying to run a Maven project (I am using IntelliJ to do the same) by using mvn install in the terminal I get the following error:

[ERROR] com.scopic.javachallenge.controllers.TeamProcessControllerTest.testSample  Time elapsed: 0 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityMapper' defined in file [C:\Users\ronit\Documents\Repos\target\classes\com\scopic\javachallenge\mappers\EntityMapper.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanIn
stantiationException: Failed to instantiate [com.scopic.javachallenge.mappers.EntityMapper]: Constructor threw exception; nested exception is java.lang.reflect.InaccessibleObjectException: Unable to make protected native java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException acces
sible: module java.base does not "opens java.lang" to unnamed module @66d1af89
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.scopic.javachallenge.mappers.EntityMapper]: Constructor threw exception; nested exception is java.lang.reflect.InaccessibleObjectException: Unable to make protected native java.lang.Object java.lang.Object.clone() throws
 java.lang.CloneNotSupportedException accessible: module java.base does not "opens java.lang" to unnamed module @66d1af89
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected native java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException accessible: module java.base does not "opens java.lang" to unnamed module @66d1af89

The class in question, EntityMapper, looks like this simplified:

public class EntityMapper {

    private final MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

    private final MapperFacade mapper = mapperFactory.getMapperFacade();

    public EntityMapper() throws Exception {
        // public constructor
    }

}

I have tried multiple solutions from other sites, including using throws NullPointException() in the default constructor and changing the VM options to include --add-opens java.base/java.lang=ALL-UNNAMED when running mvn install. None of these have succeeded. I do not know if there is something I am missing in my code or if I have not set up the environment properly - but what does confuse me is the fact that EntityMapper does not actually seem to be getting called anywhere. How can I go about fixing this?

EDIT: After adding the --add-opens to the pom.xml file, mvn install runs fine. However, the same error now appears for mvn spring-boot:run. How does this get fixed, and why does the pom.xml solution only work for the first command?

CodePudding user response:

Try to add '--add-opens java.base/java.lang=ALL-UNNAMED' in your pom.xml. See: https://stackoverflow.com/a/71296829/13774637

  • Related