1

My helloworld pf war failed to deploy in Tomcat10.1.7 and jdk17 with errors. I followed these links to configure pom.xml. My app has a simple file index.xhtml.

SEVERE [Catalina-utility-1] org.apache.myfaces.webapp.FacesInitializerImpl.initFaces An error occurred while initializing MyFaces: Could not find a backup for factory jakarta.faces.application.ApplicationFactory. 
    java.lang.IllegalStateException: Could not find a backup for factory jakarta.faces.application.ApplicationFactory. 
        at jakarta.faces.FactoryFinderInstance.notNullFactory(FactoryFinderInstance.java:496)
        at jakarta.faces.FactoryFinderInstance.getFactory(FactoryFinderInstance.java:190)
        at jakarta.faces.FactoryFinder.getFactory(FactoryFinder.java:263)
        at org.apache.myfaces.context.servlet.FacesContextImplBase.getApplication(FacesContextImplBase.java:173)
        at org.apache.myfaces.context.servlet.FacesContextImplBase.getELContext(FacesContextImplBase.java:235)
        at jakarta.faces.component.UIViewRoot.setLocale(UIViewRoot.java:1525)
        at org.apache.myfaces.webapp.FacesInitializerImpl._createFacesContext(FacesInitializerImpl.java:498)
        at org.apache.myfaces.webapp.FacesInitializerImpl.initStartupFacesContext(FacesInitializerImpl.java:467)
        at org.apache.myfaces.webapp.FacesInitializerImpl.initFaces(FacesInitializerImpl.java:135)
        at org.apache.myfaces.webapp.StartupServletContextListener.contextInitialized(StartupServletContextListener.java:54)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4453)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4885)                
SEVERE [Catalina-utility-1] com.sun.faces.config.ConfigureListener.contextInitialized Critical error during deployment: 
    com.sun.faces.config.ConfigurationException: CONFIGURATION FAILED! Cannot invoke "jakarta.faces.application.ResourceHandler.createViewResource(jakarta.faces.context.FacesContext, String)" because "this.resourceHandler" is null
        at com.sun.faces.config.ConfigManager.initialize(ConfigManager.java:319)
        at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:179)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4451)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4885)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        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:713)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:975)
        at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1949)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
        at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:123)              
    Caused by: java.lang.NullPointerException: Cannot invoke "jakarta.faces.application.ResourceHandler.createViewResource(jakarta.faces.context.FacesContext, String)" because "this.resourceHandler" is null
        at com.sun.faces.facelets.impl.DefaultResourceResolver.resolveUrl(DefaultResourceResolver.java:34)
        at com.sun.faces.facelets.impl.DefaultFaceletFactory.init(DefaultFaceletFactory.java:107)
        at com.sun.faces.application.ApplicationAssociate.createFaceletFactory(ApplicationAssociate.java:647)
        at com.sun.faces.application.ApplicationAssociate.initializeFacelets(ApplicationAssociate.java:338)
        at com.sun.faces.application.ApplicationAssociate.getCompiler(ApplicationAssociate.java:373)
        at com.sun.faces.config.processor.FaceletTaglibConfigProcessor.process(FaceletTaglibConfigProcessor.java:214)
        at com.sun.faces.config.ConfigManager.initialize(ConfigManager.java:310)

I searched those errors, but I couldn't figure out what was missing. pom.xml (I tried to add my pom.xml, but I was unable.)

<dependencies>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>jakarta.faces</artifactId>
        <version>4.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.weld.servlet</groupId>
        <artifactId>weld-servlet-shaded</artifactId>
        <version>5.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>jakarta.servlet.jsp.jstl</groupId>
        <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
        <version>2.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>11.0.0</version>
        <classifier>jakarta</classifier>
    </dependency>
</dependencies>
<?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_4_0.xsd"
    version="4.0">

    <display-name>My PrimeFaces Application</display-name>
    <servlet>
        <servlet-name>faces-servlet</servlet-name>
        <servlet-class>jakarta.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>faces-servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

The project link: https://github.com/vtrance/pf2

zforgo
  • 2,508
  • 2
  • 14
  • 22
dev
  • 11
  • 1
  • 3
  • I'd guess either faces-config.xml or web.xml is wrong. That cannot be answered without knowing their content. Both can be empty for a minimal working JSF project. If that works then just add back one by one the original configuration in order to nail down the cause. Also the optional `primefaces` dependency can be omitted for a minimal working JSF project. – BalusC Apr 18 '23 at 15:03
  • I removed primefaces from pom, and I had 3 empty files web.xml, beans.xml, faces-config.xml under WEB-INF as minimal. However, it threw an error SAXParseExption ... Premature end of file for web.xml when the war was deployed. Thus I added some config back to web.xml (updated my question), the other 2 were still empty. Then I got the previous error again. – dev Apr 18 '23 at 17:33
  • Added a project link in the question. – dev Apr 24 '23 at 14:06
  • 1
    I tried your project. It appears that this is not the full stack trace. Much earlier in the server log there's a WARN line which says that Weld skipped initialization because the BDA is empty. And then there's an exception coming from Mojarra that CDI is not available. And then as a domino effect all kinds of exceptions are thrown as consequence of this, including the NPE included in your question. But this is **not** the root cause of your problem. It's merely a side effect. You need to focus on that WARN line. Once that's resolved, all of the rest will automagically disappear. – BalusC Apr 25 '23 at 11:38
  • 2
    BalusC, I added a dummy class with @Named to a project, and the error disappeared. Thank you for your help! – dev Apr 25 '23 at 18:52
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – user207421 Jul 04 '23 at 11:18
  • Nope, @user207421. The NPE is not coming from an user-defined class, it's coming from a 3rd party library. See answer below. This is in *no way* covered by the duplicate you tried to propose. – BalusC Jul 04 '23 at 11:30

1 Answers1

0

This has been reported at Mojarra as issue 5232. The analysis is as follows:

This is not the root cause. Much earlier in the server log there's a WARN line coming from Weld that the BDA is empty followed by an exception from Mojarra that CDI is unavailable. Mojarra basically needs to fail immediately when CDI is unavailable instead of continuing initialization which would then throw various kinds of exceptions, including the one reported in this issue.

This is the clue much earlier in server log:

[snip]

org.jboss.weld.environment.servlet.WeldServletLifecycle initialize
WARN: WELD-ENV-000028: Weld initialization skipped - no bean archive found

[snip]

Caused by: java.lang.IllegalStateException: CDI is not available
    at com.sun.faces.util.Util.getCdiBeanManager(Util.java:1494)
    at com.sun.faces.application.applicationimpl.ExpressionLanguage.addELResolver(ExpressionLanguage.java:106)
    at com.sun.faces.application.ApplicationImpl.addELResolver(ApplicationImpl.java:208)
    at jakarta.faces.application.ApplicationWrapper.addELResolver(ApplicationWrapper.java:523)
    at org.jboss.weld.environment.servlet.jsf.WeldApplication.<init>(WeldApplication.java:71)
    at org.jboss.weld.environment.servlet.jsf.WeldApplicationFactory.getApplication(WeldApplicationFactory.java:46)
    at com.sun.faces.application.InjectionApplicationFactory.getApplication(InjectionApplicationFactory.java:61)
    at com.sun.faces.config.InitFacesContext.getApplication(InitFacesContext.java:109)
    at com.sun.faces.lifecycle.ClientWindowFactoryImpl.<init>(ClientWindowFactoryImpl.java:39)
    ... 49 more

[snip]

It says literally:

no bean archive found

This can be solved by simply adding a WEB-INF/beans.xml file to the project. But if you already have one and it's not an empty file, then this can happen when you have configured the bean-discovery-mode as annotated

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_0.xsd"
       bean-discovery-mode="annotated"
       version="4.0">
</beans>

That's OK, but you'll then need at least a class with a CDI managed bean defining annotation in the project in order to activate CDI. That can be any @Dependent or @XxxScoped bean.

For example one with @FacesConfig which will guaranteedly activate Faces even when web.xml and faces-config.xml are absent:

package com.stackoverflow.q76045851;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.faces.annotation.FacesConfig;

@ApplicationScoped
@FacesConfig
public class ApplicationConfig {
    // ...
}

Else, if you don't want any one, then you'll need to reconfigure bean-discovery-mode to all in order to activate CDI nonetheless.

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_0.xsd"
       bean-discovery-mode="all"
       version="4.0">
</beans>

For Faces itself, you'll need at least one of the following in order to explicitly activate Faces, so that among others <h:xxx> components in XHTML files will work:

  • A physical WEB-INF/faces-config.xml file, it can be empty.
  • An explicit FacesServlet entry and mapping in WEB-INF/web.xml, preferably on *.xhtml.
  • A CDI managed bean with @FacesConfig annotation, usually @ApplicationScoped.

If you have none of these, then there's no guarantee that Faces will be initialized on an empty project.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555