Gradle Eclipse JSF项目java.lang.ClassNotFoundException javax.faces.webapp.FacesServlet

时间:2018-03-13 00:10:02

标签: java eclipse gradle jsf-2.2 eclipse-wtp

我在Eclipse中使用Gradle插件来创建一个JSF项目。我在Tomcat 9上运行该项目。当我尝试在Eclipse中运行服务器时,我得到 java.lang.ClassNotFoundException:javax.faces.webapp.FacesServlet 异常。

我的gradle.build看起来像这样:

// 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'

我的web.xml如下所示:

<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>

我可以看到gradle已将正确的库添加到我的资源中: Project and External Dependencies

我想不出任何其他错误的东西。任何帮助表示赞赏。

更新:如果我从项目构建WAR文件,然后将该WAR作为新的动态Web项目导入,它运行正常,没有错误。所以,这里显然还有其他一些问题,可能与Gradle有关。

UPDATE2: 堆栈跟踪:

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

班级位置: enter image description here

1 个答案:

答案 0 :(得分:0)

改变&#34;实施&#34; to&#34;编译&#34;对于所需的库来解决问题。

/*
 * 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'