Java Jar 文件无法在 tomcat webapps 中加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21950908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:35:20  来源:igfitidea点击:

Jar file failed to load in tomcat webapps

javajartomcat7maven-3

提问by Rajashekhar

This is what the error i got when i try to deploy maven project to tomcat mvn tomcat7:deployError : INFO: validateJarFile(D:\Softwares\tomcat\apache-tomcat-7.0.50\webapps\myWebApp_ 1\WEB-INF\lib\javax.servlet-api-3.0.1.jar) - jar not loaded. See Servlet Spec 3. 0, section 10.7.2. Offending class: javax/servlet/Servlet.class

这就是我尝试将 maven 项目部署到 tomcat 时 遇到的错误mvn tomcat7:deploy错误: 信息:validateJarFile(D:\Softwares\tomcat\apache-tomcat-7.0.50\webapps\myWebApp_1\WEB-INF\ lib\javax.servlet-api-3.0.1.jar) - jar 未加载。请参阅 Servlet 规范 3.0,第 10.7.2 节。违规类:javax/servlet/Servlet.class

But the javax.servlet-api-3.0.1.jar is there in WEB-INF\lib Thanks

但是 WEB-INF\lib 中有 javax.servlet-api-3.0.1.jar 谢谢

采纳答案by Saif Asif

INFO: validateJarFile(D:\Softwares\tomcat\apache-tomcat-7.0.50\webapps\myWebApp_ 1\WEB-INF\lib\javax.servlet-api-3.0.1.jar) - jar not loaded

信息:validateJarFile(D:\Softwares\tomcat\apache-tomcat-7.0.50\webapps\myWebApp_1\WEB-INF\lib\javax.servlet-api-3.0.1.jar) - jar 未加载

Servlet3.0 is already shipped with the tomcat inside its lib folder and by default tomcat will always load the servlet jar present there. Thats why you are getting the warning that tomcat is not loading your jar inside the project.

Servlet3.0 已经在其 lib 文件夹中随附了 tomcat,默认情况下 tomcat 将始终加载存在于那里的 servlet jar。这就是为什么你会收到警告,说 tomcat 没有在项目中加载你的 jar。

Simple solution : If you are using maven, set its scope as providedinside the pom.xml and maven will not place it in the WEB-INF/lib of your project. Something like

简单的解决方案:如果您使用的是 maven,请将其范围设置为providedpom.xml 内,maven 不会将其放置在您项目的 WEB-INF/lib 中。就像是

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

回答by Sanjay Bharwani

Precise explanation by Saif asif.

Saif asif 的精确解释。

I was using gradle as the build tool. It worked for me to exclude the tomcat-servlet-api.jar which is by default provided by tomcat.

我使用 gradle 作为构建工具。排除tomcat默认提供的tomcat-servlet-api.jar对我有用。

You need to know which of your dependency is transitively adding tomcat-servlet-api.jar to the war and then exclude it using following

您需要知道您的哪个依赖项正在将 tomcat-servlet-api.jar 添加到 war 中,然后使用以下方法将其排除

 compile ('your dependency goes here') {
        exclude module:'tomcat-servlet-api'
    }

回答by Surendra Verma

Best Solution : remove that jar(i.e javax.servlet-api-3.0.1.jar) from lib

最佳解决方案:从 lib 中删除那个 jar(即 javax.servlet-api-3.0.1.jar)

回答by Tim

This is just an warning that appears in your log, not an error. Your project will still deploy if you ignore this.

这只是出现在日志中的警告,而不是错误。如果您忽略这一点,您的项目仍将部署。

You can get get rid of this error by rebuilding your .war using maven and copying it into \webapps\ removing your old files.

您可以通过使用 maven 重建 .war 并将其复制到 \webapps\ 中删除旧文件来消除此错误。