java.lang.ClassNotFoundException:org.apache.jsp.index_jsp

bar*_*lay 17 jsp maven

我有一个旧的struts 1应用程序,它总是使用Ant构建,我正在转换为使用Maven.我的应用程序的结构是模块化的,在包含模块中具有依赖关系管理.包含模块的dep mgmt部分包含:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        ...
    </dependencies>
</dependencyManagement>
Run Code Online (Sandbox Code Playgroud)

当我构建战争(我package从包含模块运行Maven作业)并使用来自intelliJ的战争运行Tomcat配置时,我在浏览器中看到以下内容:

org.apache.jasper.JasperException: org.apache.jasper.JasperException: Unable to load class for JSP
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:161)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:340)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause
org.apache.jasper.JasperException: Unable to load class for JSP
org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:630)
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:149)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:340)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause
java.lang.ClassNotFoundException: org.apache.jsp.index_jsp
java.net.URLClassLoader$1.run(URLClassLoader.java:202)
java.security.AccessController.doPrivileged(Native Method)
java.net.URLClassLoader.findClass(URLClassLoader.java:190)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:134)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:66)
org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:628)
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:149)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:340)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Run Code Online (Sandbox Code Playgroud)

我在搜索时可以找到的所有内容都表明原因是javax.servlet jar中存在版本冲突,但我的WEB-INF/lib目录中根本没有任何内容.我一直在使用的范围尝试provided,compile甚至install/pom,但那些没有做任何帮助.

tomcat日志中没有其他内容.

我确认正在编译jsps.我正在使用jspc-maven-plugin.

在我的子模块(不是包含的)pom.xml中,我有:

<build>
    <!-- default goal to run if somebody doesn't pick one (rarely matters) -->
    <defaultGoal>install</defaultGoal>

    <!-- resources and filtering - also see configuration under maven-war-plugin below -->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <filters>
        <filter>src/main/resources/${env}.properties</filter>
        <filter>src/main/resources/${env}.tokens</filter>
    </filters>

    <plugins>
        <!-- begin - precompiling jsps -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jspc-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>jspc</id>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <warSourceDirectory>${basedir}/src/main/webroot</warSourceDirectory>
                        <inputWebXml>${basedir}/src/main/webroot/WEB-INF/web.xml</inputWebXml>
                        <!--<workingDirectory>${basedir}/src/main/webroot</workingDirectory>-->
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- end - precompiling jsps -->

        <!-- used to build warfiles -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.0</version>
            <configuration>
                <webResources>
                    <resource>
                        <directory>${pom.basedir}/src/main/webroot</directory>
                        <filtering>true</filtering>
                        <!--<targetPath>WEB-INF</targetPath>-->
                    </resource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

我可以在maven构建输出中看到正在编译jsps.但我仍然得到JasperException.

bar*_*lay 18

我发布这篇文章已经有一段时间了,但我想我会说明我是怎么想出来的(我记得最好).

我做了一个Maven依赖树来查找依赖冲突,我删除了依赖项中排除的所有冲突,例如:

<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging-api</artifactId>
    <version>1.1</version>
    <exclusions>
        <exclusion>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>
Run Code Online (Sandbox Code Playgroud)

此外,我使用了providedjavax.servlet依赖项的范围,以免在运行应用程序时引入与Tomcat提供的内容的额外冲突.

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.1</version>
    <scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

HTH.


Joh*_*ler 8

对我不起作用的其他答案的补充:在我的情况下,由于权限错误而发生错误.当tomcat以root身份运行时,项目已部署,稍后当作为tomcat用户启动时,我从问题标题中得到错误.

我的解决方案是在unix系统上设置正确的权限,例如:

cd <tomcat-dir>
chown -R <tomcat-user> *
Run Code Online (Sandbox Code Playgroud)