How to properly install and configure JSF libraries via Maven?

When you’re facing a “weird” exception suggesting that classes/methods/files/components/tags are absent or different while they are seemingly explicitly included in the web application such as the ones below,

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/faces/webapp/FacesServlet

java.util.MissingResourceException: Can’t find javax.faces.LogStrings bundle

com.sun.faces.vendor.WebContainerInjectionProvider cannot be cast to com.sun.faces.spi.InjectionProvider

com.sun.faces.config.ConfigurationException: CONFIGURATION FAILED

The tag named inputFile from namespace http://xmlns.jcp.org/jsf/html has a null handler-class defined.

java.lang.NullPointerException at javax.faces.CurrentThreadToServletContext.getFallbackFactory

java.lang.AbstractMethodError at javax.faces.application.ViewHandlerWrapper.getWebsocketURL

java.lang.NullPointerException at com.sun.faces.config.InitFacesContext.cleanupInitMaps

or when you’re facing “weird” runtime behavior such as broken HTTP sessions (jsessionid appears in link URLs over all place), and/or broken JSF view scope (it behaves as request scoped), and/or broken CSS/JS/image resources, then the chance is big that the webapp’s runtime classpath is polluted with duplicate different versioned JAR files.

In your specific case with the ClassFormatError on the FacesServlet, it means that the JAR file containing the mentioned class has been found for the first time is actually a “blueprint” API JAR file, intented for implementation vendors (such as developers working for Mojarra and MyFaces). It contains class files with only class and method signatures, without any code bodies and resource files. That’s exactly what “absent code attribute” means. It’s purely intented for javadocs and compilation.

Always mark server-provided libraries as provided

All dependencies marked “Java Specifications” in Maven and having -api suffix in the artifact ID are those blueprint APIs. You should absolutely not have them in the runtime classpath. You should always mark them <scope>provided</scope> if you really need to have it in your pom. A well known example is the Java EE (Web) API:

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-web-api</artifactId>
    <version><!-- 7.0 or 8.0 or newer --></version>
    <scope>provided</scope>
</dependency>

If the provided scope is absent, then this JAR will end up in webapp’s /WEB-INF/lib, causing all trouble you’re facing now. This JAR also contains the blueprint class of FacesServlet.

In your specific case, you have an unnecessary JSF API dependency:

<dependency>
    <groupId>javax.faces</groupId>
    <artifactId>javax.faces-api</artifactId>
</dependency>

This is causing trouble because this contains the blueprint class of FacesServlet. Removing it and relying on a provided Java EE (Web) API as shown above should solve it.

Tomcat as being a barebones JSP/Servlet container already provides JSP, Servlet and EL (and since 8 also WebSocket) out the box. So you should mark at least jsp-api, servlet-api, and el-api as provided. Tomcat only doesn’t provide JSF (and JSTL) out the box. So you’d need to install it via the webapp.

Full fledged Java EE servers such as WildFly, TomEE, GlassFish, Payara, WebSphere, etc already provide the entire Java EE API out the box, including JSF. So you do absolutely not need to install JSF via the webapp. It would only result in conflicts if the server already provides a different implementation and/or version out the box. The only dependency you need is the javaee-web-api exactly as shown here above.

Installing JSF on Tomcat 9 or older

There are two JSF implementations: Mojarra and MyFaces. You should choose to install one of them and thus not both.

Installing Mojarra 2.3 on Tomcat 9 or older:

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>jakarta.faces</artifactId>
    <version><!-- Check https://eclipse-ee4j.github.io/mojarra --></version>
</dependency>

You can also check org.glassfish:jakarta.faces repository for current latest 2.3.x release version (which is currently 2.3.16). See also Mojarra installation instructions for other necessary dependencies (CDI, BV, JSONP).

Installing MyFaces 2.3 on Tomcat 9 or older:

<dependency>
    <groupId>org.apache.myfaces.core</groupId>
    <artifactId>myfaces-impl</artifactId>
    <version><!-- Check http://myfaces.apache.org --></version>
</dependency>

You can also check org.apache.myfaces.core:myfaces-impl repository for current latest 2.3.x release version (which is currently 2.3.9).

Note that Tomcat 6 as being Servlet 2.5 container supports max JSF 2.1.

Don’t forget to install JSTL API along, by the way. This is also absent in Tomcat.

<dependency>
    <groupId>jakarta.servlet.jsp.jstl</groupId>
    <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
    <version><!-- Check https://mvnrepository.com/artifact/jakarta.servlet.jsp.jstl/jakarta.servlet.jsp.jstl-api --></version>
</dependency>

Also note that since JSF 2.3, CDI has become a required dependency. This is available out the box in normal Java EE servers but not on servletcontainers such as Tomcat. In this case head to How to install and use CDI on Tomcat?

Installing JSF on Tomcat 10 or newer

You’ll need a minimum JSF version of 3.0 instead of 2.3 because the javax.* package has been renamed to jakarta.* since 3.0 only.

Installing Mojarra 3.0 on Tomcat 10 or newer:

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>jakarta.faces</artifactId>
    <version><!-- Check https://eclipse-ee4j.github.io/mojarra --></version>
</dependency>

You can also check org.glassfish:jakarta.faces repository for current latest 3.0.x release version (which is currently 3.0.1). See also Mojarra installation instructions for other necessary dependencies (CDI, BV, JSONP).

Installing MyFaces 3.0 on Tomcat 10 or newer:

<dependency>
    <groupId>org.apache.myfaces.core</groupId>
    <artifactId>myfaces-impl</artifactId>
    <version><!-- Check http://myfaces.apache.org --></version>
</dependency>

You can also check org.apache.myfaces.core:myfaces-impl repository for current latest 3.0.x release version (which is currently 3.0.1).

Don’t forget to install JSTL API along, by the way. This is also absent in Tomcat.

<dependency>
    <groupId>jakarta.servlet.jsp.jstl</groupId>
    <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
    <version><!-- Check https://mvnrepository.com/artifact/jakarta.servlet.jsp.jstl/jakarta.servlet.jsp.jstl-api 2.x --></version>
</dependency>

Also note that since JSF 2.3, CDI has become a required dependency. This is available out the box in normal Java EE servers but not on servletcontainers such as Tomcat. In this case head to How to install and use CDI on Tomcat?

See also:

Leave a Comment