Search code examples
javaeclipsegradlejsf-2.2eclipse-wtp

Gradle Eclipse JSF Project java.lang.ClassNotFoundException javax.faces.webapp.FacesServlet


I am using the Gradle plugin in Eclipse to create a JSF project. I am running the project on Tomcat 9. When I attempt run on server in Eclipse, I get the java.lang.ClassNotFoundException: javax.faces.webapp.FacesServlet exception.

My gradle.build looks like this:

// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'
apply plugin: 'war'
apply plugin: 'eclipse'
apply plugin: 'eclipse-wtp'

repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    api 'org.apache.commons:commons-math3:3.6.1'
    implementation 'com.google.guava:guava:21.0'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'

    //Jersey
    implementation 'org.glassfish.jersey.containers:jersey-container-servlet:2.26'
    implementation 'org.glassfish.jersey.inject:jersey-hk2:2.26'
    implementation 'org.glassfish.jersey.core:jersey-common:2.26'

    //JAXB
    implementation 'org.glassfish.jersey.media:jersey-media-json-jackson:2.26'

    //Javax Servlet
    implementation 'javax.servlet:javax.servlet-api:3.1.0'

    //JSF
    implementation 'org.glassfish:javax.faces:2.2.16'
}

eclipse {
    wtp {
        facet {
            facet name: "java", version: "1.8"          // Java version
            facet name: "jst.web", version: "3.1"       // Dynamic Web Application
            facet name: "jst.jsf", version: "2.2"       // Java Server Faces
            facet name: "wst.jsdt.web", version: "1.0"  // JavaScript
        }
    }
}

project.webAppDirName='WebContent'

My web.xml looks like this:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4" id="WebApp_ID">
    <display-name>com.xxx</display-name>
    <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>*.xhtml</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

I can see that gradle has added the correct library to my resources: Project and External Dependencies

I cannot think of anything else that could be wrong. Any help is appreciated.

UPDATE: If I build a WAR file from the project, and then import that WAR as a new Dynamic Web Project, it runs fine, with no errors. So, there is obviously some other issue going on here, probably related to Gradle.

UPDATE2: Stack Trace:

SEVERE: Servlet [Faces Servlet] in web application [/com.foo.bar] threw load() exception
java.lang.ClassNotFoundException: javax.faces.webapp.FacesServlet
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1275)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1104)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:540)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:521)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:150)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1032)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:971)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4765)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5075)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1427)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1417)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:943)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:839)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1427)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1417)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:943)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:258)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:770)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:682)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:353)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:493)

Build Path: Build Path

Class location: enter image description here


Solution

  • Changing "implementation" to "compile" for the required libraries fixed the issue.

    /*
     * This build file was generated by the Gradle 'init' task.
     *
     * This generated file contains a sample Java Library project to get you started.
     * For more details take a look at the Java Libraries chapter in the Gradle
     * user guide available at https://docs.gradle.org/3.5/userguide/java_library_plugin.html
     */
    
    // Apply the java-library plugin to add support for Java Library
    apply plugin: 'java-library'
    apply plugin: 'war'
    apply plugin: 'eclipse-wtp'
    
    // In this section you declare where to find the dependencies of your project
    repositories {
        // Use jcenter for resolving your dependencies.
        // You can declare any Maven/Ivy/file repository here.
        jcenter()
    }
    
    dependencies {
        // This dependency is exported to consumers, that is to say found on their compile classpath.
        api 'org.apache.commons:commons-math3:3.6.1'
    
        // This dependency is used internally, and not exposed to consumers on their own compile classpath.
        implementation 'com.google.guava:guava:21.0'
    
        // Use JUnit test framework
        testImplementation 'junit:junit:4.12'
    
        //Jersey
        compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.26'
        compile 'org.glassfish.jersey.inject:jersey-hk2:2.26'
        compile 'org.glassfish.jersey.core:jersey-common:2.26'
    
        //JAXB
        compile 'org.glassfish.jersey.media:jersey-media-json-jackson:2.26'
    
        //Javax Servlet
        compile 'javax.servlet:javax.servlet-api:3.0.1'
    
        //JSF
        compile 'org.glassfish:javax.faces:2.2.16'
    
        compile 'javax.inject:javax.inject:1'
    
        compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.25.1'
    }
    
    eclipse {
        wtp {
            facet {
    
                facets = []
                facet name: "java", version: "1.8"          // Java version
                facet name: "jst.web", version: "3.0"       // Dynamic Web Application
                facet name: "jst.jsf", version: "2.2"       // Java Server Faces
                facet name: "wst.jsdt.web", version: "1.0"  // JavaScript
            }
        }
    }
    
    project.webAppDirName='WebContent'