java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
我正在开发一个 servlet,它接收包含多个文件内容的多部分请求,并且我正在使用 apache commons 文件上传库。
当我调用 parseRequest(request);
方法时,servlet 抛出以下异常:
GRAVE: Servlet.service() for servlet DiffOntology threw exception
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
at org.apache.commons.fileupload.servlet.ServletRequestContext.getContentType(ServletRequestContext.java:73)
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:882)
at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:349)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
at DiffOntology.doPost(DiffOntology.java:38)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:738)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
我将所有库放在 WEB-INF/lib 中。
编辑:
servlet-api.jar 位于正确的目录(tomcat/lib)中,所有其他库位于 WEB-INF/lib 中
我认为问题可能如下: 我正在 Eclipse 中开发这个 Web 项目,并在类路径中导入了文件上传库。
怎么不起作用呢?
我很绝望!!
I'm developing a servlet that receives a multipart request with content of multiple files, and I'm using apache commons file upload libraries.
When I call parseRequest(request);
method servlet throws following exception:
GRAVE: Servlet.service() for servlet DiffOntology threw exception
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
at org.apache.commons.fileupload.servlet.ServletRequestContext.getContentType(ServletRequestContext.java:73)
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:882)
at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:349)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
at DiffOntology.doPost(DiffOntology.java:38)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:738)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
I put all libraries in WEB-INF/lib.
EDIT:
servlet-api.jar is in the correct directory (tomcat/lib) and all others libraries are in WEB-INF/lib
I think maybe the problem could be the following:
I'm developing this web project in Eclipse, and I imported file uploads libraries in the classpath.
How it doesn't work?
I'm desperate!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
当您将特定于服务器的库放置在 Web 应用程序的
/WEB-INF/lib
或可能的JRE/lib
中时,可能会发生这种情况。您很有可能将 Tomcat 的/lib/servlet-api.jar
复制到那里。你不应该这样做。这只会导致类路径中的冲突,从而导致此类错误,并且会使您的 web 应用程序不可移植(即它只能在 Tomcat 上运行,您不能在其他服务器(如 Glassfish、JBoss AS、Websphere)上运行它, ETC)。您应该将特定于服务器的库保留在其默认位置。从任何特定于服务器的库中清除/WEB-INF/lib
并从任何第 3 方库中清除JRE/lib
。您可能在那里复制了特定于服务器的库,因为您无法编译您的 servlet。复制
/WEB-INF/lib
中的库是错误的解决方案。您基本上应该只在编译时类路径中指定这些库。由于您使用的是 Eclipse,因此可以轻松完成此操作:首先在 Servers 视图中添加 Tomcat,然后将您的 web 应用程序项目与集成的 Tomcat 实例关联。这样 Eclipse 就会自动将服务器特定的库添加到项目的构建路径中。在全新的 Web 项目中,您可以在项目创建向导期间选择服务器。在现有的 Web 项目中,您可以在项目属性的目标运行时部分中对其进行修改。另请参阅:
This can happen when you've placed server-specific libraries in the webapp's
/WEB-INF/lib
or probablyJRE/lib
. Big chance that you copied Tomcat's/lib/servlet-api.jar
into there. You shouldn't do that. This would only lead to collisions in the classpath which leads to this kind of errors and it will make your webapp unportable (i.e. it is tied to run on Tomcat only, you can't run it at another servers like Glassfish, JBoss AS, Websphere, etc). You should keep the server-specific libraries at their default location. Cleanup the/WEB-INF/lib
from any server-specific libraries and cleanupJRE/lib
from any 3rd party libraries.You probably copied server-specific libraries there because you wasn't able to compile your servlets. Copying the libraries in
/WEB-INF/lib
is the wrong solution. You should basically just specify those libraries in the compiletime classpath. Since you're using Eclipse, this can be done easily: first add Tomcat in Servers view, then associate your webapp project with the integrated Tomcat instance. This way Eclipse will automatically add the server-specific libraries to the project's buildpath. On a brand new web project you can choose the server during project creation wizard. On existing web projects, you can modify it in Targeted Runtimes section in project's properties.See also:
您必须错误地将 commons-fileupload.jar 复制到
JRE/lib/ext
、JRE/lib/endorsed
,或者将其放置在无法访问的类路径中servlet API。使用-verbose:class
启动 JVM,它将打印哪个类路径加载了ServletFileUpload
类。如果该类是从WEB-INF/lib
以外的任何位置加载的,则需要将其删除。You must have incorrectly copied commons-fileupload.jar to
JRE/lib/ext
,JRE/lib/endorsed
, or otherwise placed it on a classpath that does not have visibility to the servlet APIs. Start the JVM with-verbose:class
, which will print which classpath loaded theServletFileUpload
class. If the class is loaded from anywhere other thanWEB-INF/lib
, you'll need to remove it.老线程但仍然可以帮助别人。
我看到测试范围中包含的依赖项中有一个 javax-servlet jar。我已经达到了提供的范围。如果您使用 Eclipse +Maven,请检查依赖关系图。
Old thread but can still help someone.
I saw that i had a javax-servlet jar in the dependency included with a test scope. I made it to provided scope. Check the dependency graph if you are using Eclipse +Maven.
当我错误地使用 Tomcat 10 Docker 映像为 Tomcat 9 构建 WAR 文件时,我收到了该错误。
我使用了一个没有特定标签的 Docker 镜像,该镜像随最新的 Tomcat 一起提供(在我的例子中为 10)(
tomcat:jdk17-temurin
)。使用 Tomcat 10 需要进行调整,如 Tomcat 10 文档所述:
就我而言,我只是通过指定具有特定 Tomcat 版本的映像(例如
tomcat:9.0.56-jdk17-temurin
)来解决该错误。I got that error when mistakenly using a Tomcat 10 Docker image for my WAR file build for Tomcat 9.
I used a Docker image without a specific tag which came with the latest Tomcat (in my case 10) (
tomcat:jdk17-temurin
).Using Tomcat 10 would need adjustments as the Tomcat 10 doc states:
In my case I simply resolved that error by specifiyed an image with a specific Tomcat version (e.g.
tomcat:9.0.56-jdk17-temurin
).从 JRE/lib 或 JRE/lib/ext 中删除任何 servlet-api.jar 或上传帮助 jar commons。这帮助我解决了问题。
Remove any servlet-api.jar or the upload helping jar commons from JRE/lib or JRE/lib/ext. This helped me fix the problem.
我也有同样的问题。我遇到这个问题的原因是因为我使用 jakarta-servlet-api 我假设我需要的类位于 jakarta 包中。但是,当我提取 api 时,我看到该包名为 javax,因此我导入了 javax.servlet.http.HttpServlet,并且它工作正常。
I had the same problem. The reason why I had this problem is that since I am using jakarta-servlet-api I assumed that the class I need is in a jakarta package. However, when I extracted the api I saw that the package is called javax, so I imported javax.servlet.http.HttpServlet instead and it worked fine.
在使用 Tomcat 9 的 Gentoo Linux 上,为了在 Eclipse 上开发 Java EE 应用程序,当我尝试从 Eclipse 的 Java EE 视角的“服务器”窗口启动服务器时,我会收到此错误消息。我尝试手动将
/usr/share/tomcat-servlet-api-4.0/lib/servlet-api.jar
添加到项目的类路径中,但没有成功。我尝试手动将该文件添加到/usr/share/tomcat-9/package.env
上的 CLASSPATH 变量中,但没有成功。在多次尝试修复该问题失败后,我的直觉告诉我,也许 Portage 发行版应该作为一个独立的守护进程运行,由 shell 而不是 Eclipse 管理,而且也许,出于开发目的,我必须安装一个普通的普通实例雄猫。于是我去官网手动下载Tomcat 9并手动解压;我使用了
/opt/apache/tomcat/tomcat9
,但理论上任何目录都可以。我就是这么做的,Tomcat终于启动成功了。tl;dr:如果您在 Gentoo 上开发 Java EE 应用程序,请不要使用 Portage 发行版,而是从官方网站手动下载并安装它。
On Gentoo Linux, using Tomcat 9, for the purposes of developing Java EE apps on Eclipse, I would get this error message when attempting to launch the server from the Servers window on Eclipse's Java EE perspective. I tried manually adding
/usr/share/tomcat-servlet-api-4.0/lib/servlet-api.jar
to my project's classpath, didn't work. I tried manually adding that file to the CLASSPATH variable on/usr/share/tomcat-9/package.env
, didn't work.After many unsuccessful attempts to fix that, my intuition told me that maybe the Portage distribution was meant to run as a standalone daemon managed from shell rather than from Eclipse, and that maybe, for development purposes, I had to install a plain vanilla instance of Tomcat. So I went to manually download Tomcat 9 from the official web page and manually extracted it; I used
/opt/apache/tomcat/tomcat9
, but in theory any directory should do. I did that, and Tomcat finally started successfully.tl;dr: if you're developing Java EE apps on Gentoo, don't use the Portage distribution, download and install it manually from the official web site instead.
检查你的Tomcat的版本,如果你使用的是10.1.24那么你必须将javax 4.0的依赖项更改为jakarta 6.0
而不是
使用
check the version of your Tomcat, if you are using 10.1.24 then you have to change the dependency of javax 4.0 to jakarta 6.0
instead of
use