2

I am completely new to JSF development and I am getting following exception while deploying to Tomcat 9.0.80:

27-Aug-2023 22:34:59.067 SEVERE [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log Servlet.init() for servlet [Faces Servlet] threw exception
    27-Aug-2023 23:30:14.803 SEVERE [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class [com.sun.faces.config.ConfigureListener]
java.lang.RuntimeException: javax.faces.FacesException: Unable to find CDI BeanManager
    at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:315)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4462)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4914)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:171)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:683)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:658)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:662)
    at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1782)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:294)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:460)
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:408)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:294)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:468)
    at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
    at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
    at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1408)
    at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
    at sun.rmi.transport.Transport$1.run(Transport.java:200)
    at sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javax.faces.FacesException: Unable to find CDI BeanManager
    at com.sun.faces.el.ELUtils.tryAddCDIELResolver(ELUtils.java:312)
    at com.sun.faces.el.ELUtils.buildFacesResolver(ELUtils.java:242)
    at com.sun.faces.application.ApplicationAssociate.initializeELResolverChains(ApplicationAssociate.java:484)
    at com.sun.faces.application.ApplicationImpl.performOneTimeELInitialization(ApplicationImpl.java:1404)
    at com.sun.faces.application.ApplicationImpl.getELResolver(ApplicationImpl.java:526)
    at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:256)
    ... 47 more

I searched on net (including stackoverflow) for answers, but all of them simply go above my head. Some of the links that I looked at (whose solutions apparently do not work for me):

  1. javax.faces.FacesException Unable to find CDI BeanManager
  2. Wildfly starts with error. javax.faces.FacesException: Unable to find CDI BeanManager

Here's my beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:beans="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
       version="2.0" bean-discovery-mode="annotated">

</beans>

Following is my pom.xml file

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>in.xiller.udemy.jsf</groupId>
    <artifactId>hello</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>Hello</name>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.faces</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
        </plugins>
    </build>
</project>

And, following is my facelets-config.xml (located inside src/webapp/WEB-INF):

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_3.xsd"
    version="2.3">

</faces-config>

And, here is my web.xml:

   <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>hello</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <listener>
     <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>
</web-app>

Can someone please help me understand in short and simple words:

  1. what I am doing wrong
  2. how to resolve the above issue
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Python_user
  • 1,378
  • 3
  • 12
  • 25
  • 1
    @stdunbar Thanks, no this is not a school project. I am learning JSF on my own following some tutorial on udemy. I am trying to get it up and running locally ... However, it does not seem to work. Could you please help in this regard? Can I add some CDI implementation library to make it work? – Python_user Aug 27 '23 at 18:18
  • 1
    Please try to find most recent tutorials on stuff you want to learn. Please pay attention to the version it's trying to tutor as well as the latest version actually currently available. The specific `` entry in your `web.xml` as well as the specific `/faces/*` URL pattern and the completely unnecessary JSTL fmt config strongly suggests that the tutorial you was reading was written with JSF 1.x in mind (or that the tutorial's author has no idea what he was doing) and this while JSF 2.x was already released Dec 2009. Current latest version is Faces 4.0 which was released May 2022. – BalusC Aug 28 '23 at 09:43
  • @BalusC The tutorial that I am following uses JSF 2.2 and is about at least 6 years old. I am using it just to get my feet wet in JSF. Didn't know it could cause so many issues. – Python_user Aug 29 '23 at 20:08
  • @BalusC But, it is good to struggle initially and learn. However, in the interest of time, I will try to find an authoritative book or guide on JSF 4 – Python_user Aug 29 '23 at 20:09
  • @stdunbar that is a categorically incorrect statement. The largest e-commerce sites in the entire world use server side rendering – Jonathan S. Fisher Aug 31 '23 at 21:35

1 Answers1

0

Since you're using Tomcat (servlet container), then you will get this error because Tomcat by default does not support full Java EE features, including CDI.

You can include Weld (a CDI implementation) via the following Maven dependency:

<!-- https://mvnrepository.com/artifact/org.jboss.weld.servlet/weld-servlet -->
<dependency>
    <groupId>org.jboss.weld.servlet</groupId>
    <artifactId>weld-servlet</artifactId>
    <version>2.4.8.Final</version>
</dependency>

This is from the JBoss Official Website as well. It may be worth a look and using as it is probably more current.

<dependency>
    <groupId>org.jboss.weld.servlet</groupId>
    <artifactId>weld-servlet-core</artifactId>
    <version>5.1.1.SP2</version>
</dependency>

I have not touched JSF in years, so these are merely suggestions.

Try adding a listener to web.xml once you add the required depedencies:

<listener>
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>

Side Note: If you are not married to Tomcat, I would suggest using a full EE implementation server like WildFly or Payara. It comes with all these features out of the box.

cela
  • 2,352
  • 3
  • 21
  • 43
  • Hi @cela, this solution did not work ... Still getting that exception ... Do I need to make any change to beans.xml? – Python_user Aug 27 '23 at 18:29
  • Nope, still didn't work ... In the meantime, I will be going through JBoss link that you have mentioned. Although, it will take me quite sometime to fully understand what is happening. And, no ... I am not married to Tomcat :) ... will consider using TomEE or Wildfly instead – Python_user Aug 27 '23 at 18:44
  • @Python_user Is it the same exception? – cela Aug 27 '23 at 18:44
  • yes, it is the same exception ... as mentioned in my pom file, my compiler source and target are set to 1.8 and I have SDK 8 in my local system too (along with 11 and 18). Although if I type `mvn -v`, I get following as output: `Java version: 18.0.1.1, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-18.0.1.1` ... Is that an issue? – Python_user Aug 27 '23 at 18:55
  • 1
    I started using Wildfly and everything works just smoothly! I will try to make this work with Tomcat though – Python_user Aug 29 '23 at 19:54