I am currently migrating a project from Java 11 to Java 17, Spring 5 to Spring 6, and from RichFaces to PrimeFaces 14 (Jakarta). After making the necessary changes, I am encountering the following exception:
Exception in thread "main" java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityBean': Invocation of init method failed
at com.leo.service-ui.Main.main(Main.java:27)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityBean': Invocation of init method failed
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:222)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:422)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1798)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:600)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:337)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:975)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:971)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:625)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:335)
at com.leo.service-ui.Main.main(Main.java:24)
Caused by: java.lang.NoClassDefFoundError: jakarta/enterprise/inject/spi/el/ELAwareBeanManager
at jakarta.faces.CurrentThreadToServletContext.getFactoryFinder(CurrentThreadToServletContext.java:52)
at jakarta.faces.FactoryFinder.getFactory(FactoryFinder.java:257)
at jakarta.faces.context.FacesContext.getCurrentInstance(FacesContext.java:872)
at com.leo.service-ui.util.FacesUtil.getRequestParameter(FacesUtil.java:28)
at com.leo.service-ui.ui.beans.EntityBean.init(EntityBean.java:76)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:569)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMethod.invoke(InitDestroyAnnotationBeanPostProcessor.java:457)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:401)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:219)
... 16 more
Caused by: java.lang.ClassNotFoundException: jakarta.enterprise.inject.spi.el.ELAwareBeanManager
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
... 28 more
pom.xml
:
<dependencies>
<!-- Other existing dependencies -->
<!-- Jakarta EE dependencies -->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>14.0.6</version>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>org.primefaces.extensions</groupId>
<artifactId>primefaces-extensions</artifactId>
<version>14.0.6</version>
</dependency>
<!-- <dependency>
<groupId>org.apache.myfaces.core</groupId>
<artifactId>myfaces-impl</artifactId>
<version>4.1.0-RC2</version>
</dependency>-->
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.faces</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>jakarta.el</groupId>
<artifactId>jakarta.el-api</artifactId>
<version>6.0.1</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-shaded</artifactId>
<version>5.1.3.Final</version>
</dependency>
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>10.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Here is the relevant code where the exception occurs:
public class FacesUtil {
public static String getRequestParameter(String paramName) {
FacesContext context = FacesContext.getCurrentInstance(); // Exception occurs here
Map<String, String> params = context.getExternalContext().getRequestParameterMap();
return params.getOrDefault(paramName, null);
}
}
pom.xml
.
2. Ensured that the weld-servlet-shaded
dependency is present.
3. Added the jakarta.jakartaee-api
dependency with provided
scope.
What could be causing this java.lang.ClassNotFoundException
and how can I resolve it? Are there any additional dependencies or configurations required when migrating to Java 17, Spring 6, and PrimeFaces 14 (Jakarta)?
Any help or pointers would be greatly appreciated!
java.lang.ClassNotFoundException: jakarta.enterprise.inject.spi.el.ELAwareBeanManager
This class was since CDI 4.1 (Jakarta EE 11, which Faces 4.1 is also part of) split from the core CDI API into a supplemental CDI API artifact: jakarta.cdi.el
. See also CDI 4.1 specification section "Major Changes":
CDI 4.1 no longer specifies integration with Jakarta EE. This will now be specified in the Jakarta EE Platform, Web Profile and Core Profile specifications.
The Unified EL integration API in
BeanManager
has been deprecated and the relevant methods added to a dedicated interfaceELAwareBeanManager
, which is present in a new supplemental API artifact:jakarta.enterprise:jakarta.enterprise.cdi-el-api
.
So, in other words, in case you target a barebones servletcontainer such as Tomcat instead of a real Jakarta EE compatible server, then the following CDI 4.1 API dependency,
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<version>4.1.0</version>
</dependency>
needs to be extended as follows:
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-el-api</artifactId>
<version>4.1.0</version>
</dependency>
The alternative in your specific case is to simply bump your existing weld-servlet-shaded
dependency from 5.x to 6.x as that already transitively(!) includes the correct API classes. It's currently only still in the beta:
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-shaded</artifactId>
<version>6.0.0.Beta4</version>
</dependency>
Do note that the transitive inclusion thus means that you can safely drop the CDI API and CDI-EL API dependencies from your pom.xml
in order to keep it simple.
Unrelated to the concrete problem, I see you also have a "provided" Jakarta EE 10 dependency. This is not entirely correct when you're targeting a barebones servletcontainer instead of a certified Jakarta EE runtime. Please refer to the following links how to correctly sort out it.