程序包javax servlet annotation不存在

本文详细解析了Maven项目中使用@WebServlet注解配置Servlet时出现的错误,包括错误描述、原因分析及解决办法。指出因未导入annotations-api.jar导致的问题,并提供了将jar包添加至项目lib目录或pom.xml文件中的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

               

1、错误描述

[INFO] Scanning for projects...[INFO]                                                                         [INFO] ------------------------------------------------------------------------[INFO] Building huxs Maven Webapp 0.0.1-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ huxs ---[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent![INFO] Copying 0 resource[INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ huxs ---[INFO] Changes detected - recompiling the module![WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent![INFO] Compiling 1 source file to E:\Eclipse\workspace\huxs\target\classes[INFO] -------------------------------------------------------------[ERROR] COMPILATION ERROR : [INFO] -------------------------------------------------------------[ERROR] /E:/Eclipse/workspace/huxs/src/main/java/com/you/maven/servlet/HelloServlet.java:[5,32] 程序包javax.servlet.annotation不存在[ERROR] /E:/Eclipse/workspace/huxs/src/main/java/com/you/maven/servlet/HelloServlet.java:[20,2] 找不到符号  符号: 类 WebServlet[INFO] 2 errors [INFO] -------------------------------------------------------------[INFO] ------------------------------------------------------------------------[INFO] BUILD FAILURE[INFO] ------------------------------------------------------------------------[INFO] Total time: 2.600 s[INFO] Finished at: 2016-01-12T15:32:29+08:00[INFO] Final Memory: 11M/171M[INFO] ------------------------------------------------------------------------[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project huxs: Compilation failure: Compilation failure:[ERROR] /E:/Eclipse/workspace/huxs/src/main/java/com/you/maven/servlet/HelloServlet.java:[5,32] 程序包javax.servlet.annotation不存在[ERROR] /E:/Eclipse/workspace/huxs/src/main/java/com/you/maven/servlet/HelloServlet.java:[20,2] 找不到符号[ERROR] 符号: 类 WebServlet[ERROR] -> [Help 1][ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.[ERROR] Re-run Maven using the -X switch to enable full debug logging.[ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles:[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

2、错误原因

     在创建Servlet时,配置Servlet用的是注解方法@WebServlet,但是项目中未导入annotations-api.jar,结果导致Maven项目报错


3、解决办法

(1)将annotations-api.jar复制到src/main/webapp/WEB-INF/lib下

(2)将annotations-api.jar配置到pom.xml中


           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值