Bug解决:Unable to compile class for JSP

问题描述:

JDK17环境下,运行Tomcat7,JSP不能正常编译

访问URL资源浏览器报错500:

type Exception report

message Unable to compile class for JSP:

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP: 

如下图:

 IDE报错:

严重: Servlet.service() for servlet [jsp] in context with path [/train] threw exception [Unable to compile class for JSP: 

An error occurred at line: [54] in the generated java file: [C:\J\IdeaProjects\jsp-demo\target\tomcat\work\Tomcat\localhost\jsp-demo\org\apache\jsp\hello_jsp.java]
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory


Stacktrace:] with root cause
org.apache.jasper.JasperException: Unable to compile class for JSP: 


An error occurred at line: [54] in the generated java file: [C:\J\IdeaProjects\jsp-demo\target\tomcat\work\Tomcat\localhost\jsp-demo\org\apache\jsp\hello_jsp.java]
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory


Stacktrace:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:198)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:450)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:580)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:356)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

项目配置:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>jsp-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>

        <!--jstl-->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

<!--        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>-->

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
            </plugin>
        </plugins>
    </build>


</project>

问题解决:

通过各种关键词查询了CSDN和GitHub,除了jsp不能正常编译还有servlet不能正常编译的情况。关于该问题的产生原因,中心说法是:

jar包冲突:servlet的jar包或者jsp-api的jar包同名重复,导致编译期加载jar包加载了同名的错误jar包

解决方案:

将配置文件中的jsp-api坐标依赖注释掉之后,jsp可以正常编译,项目正常运行,浏览器正常显示。

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
<!--注释掉上面这个依赖-->

总结:

不是所有的jar包/依赖都可以随便往工程中导!

在最新版本的JDK17中,自带servlet和jsp等相关的jar包,而运行旧版本的Tomcat时,加载jar包冲突,容易导致编译版本不一致的情况出现。因此,在新版本的JDK环境下运行旧版本的插件,要注意并非所有的依赖都随便导入(/不能所有jar包都导入),否则容易出现jar包冲突导致无法正常编译的情况。

---------------------2022.4.1追更---------------------

版本冲突

如果无法编译(Unable to compile class)或找不到字节码文件(ClassNotFoundException)的问题依然出现,只能采用其他方法了,问题的根本在于tomcat7中对java编译采用的是eclipse的ecj,而在最新版本的JDK17中,这个编译jar包已经过时了。

解决方案:

方案一:tomcat7使用jsp,将JDK版本降到JDK1.7,即可解决版本冲突问题

方案二:将tomcat7升级到tomcat10,可以在JDK17环境下正常编译*_jsp文件

这里有些同学可能遇到tomcat10运行后,无法正常加载servlet的问题,这是因为maven仓库中缺少最新的tomcat10-servlet的jar包,并且已经把最新一代servlet-api的包名更新为jakarta。而你的servlet中导包还是JDK自带的javax.servlet的jar包,自然无法正常加载jakarta包。

最坑爹的是,将servlet的坐标依赖改为jakarta,并且将导包名javax换成jakarta之后,servlet.class中导包竟然识别不到。。。。。。点击包名响应cannot find declaration to go。

解决方案:

在pom文件中将原来的servlet依赖更新为:

<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-servlet-api -->
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-servlet-api</artifactId>
    <version>10.0.0-M7</version>
    <scope>provided</scope>
</dependency>

<!--
<dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
    <version>4.0.3</version>
    <scope>provided</scope>
</dependency>
-->

原来的javax依赖或者jakarta依赖注释或删除掉即可。

---------------------end------------------------

  • 11
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coder_Liufan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值