java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2892475/
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 14:10:06  来源:igfitidea点击:

java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest

javaeclipsetomcatservletsapache-commons-fileupload

提问by pAkY88

I'm developing a servlet that receives a multipart request with content of multiple files, and I'm using apache commons file upload libraries.

我正在开发一个 servlet,它接收一个包含多个文件内容的多部分请求,我正在使用 apache commons 文件上传库。

When I call parseRequest(request);method servlet throws following exception:

当我调用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)

I put all libraries in WEB-INF/lib.

我把所有的库都放在 WEB-INF/lib 中。

EDIT:

编辑:

servlet-api.jar is in the correct directory (tomcat/lib) and all others libraries are in WEB-INF/lib

servlet-api.jar 在正确的目录 (tomcat/lib) 中,所有其他库都在 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.

我认为问题可能如下:我正在 Eclipse 中开发这个 Web 项目,并且我在类路径中导入了文件上传库。

How it doesn't work?

怎么行不通?

I'm desperate!!

我很绝望!!

回答by BalusC

This can happen when you've placed server-specific libraries in the webapp's /WEB-INF/libor probably JRE/lib. Big chance that you copied Tomcat's /lib/servlet-api.jarinto 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/libfrom any server-specific libraries and cleanup JRE/libfrom any 3rd party libraries.

当您将特定于服务器的库放置在 web 应用程序/WEB-INF/lib或可能的JRE/lib. 您将Tomcat的复制/lib/servlet-api.jar到那里的可能性很大。你不应该那样做。这只会导致类路径中的冲突,从而导致此类错误,并且会使您的 webapp 不可移植(即它只能在 Tomcat 上运行,您不能在其他服务器上运行它,例如 Glassfish、JBoss AS、Websphere , 等等)。您应该将特定于服务器的库保留在其默认位置。清理的/WEB-INF/lib任何服务器的特定库和清理JRE/lib从任何第三方库。

You probably copied server-specific libraries there because you wasn't able to compile your servlets. Copying the libraries in /WEB-INF/libis 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 Serversview, 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 Runtimessection in project's properties.

您可能在那里复制了特定于服务器的库,因为您无法编译 servlet。复制库/WEB-INF/lib是错误的解决方案。您基本上应该只在编译时类路径中指定这些库。由于您使用的是 Eclipse,因此这可以轻松完成:首先在Servers视图中添加 Tomcat ,然后将您的 webapp 项目与集成的 Tomcat 实例相关联。这样,Eclipse 会自动将特定于服务器的库添加到项目的构建路径中。在全新的 Web 项目上,您可以在项目创建向导期间选择服务器。在现有的 Web 项目上,您可以在项目属性的Targeted Runtimes部分修改它。

See also:

也可以看看:

回答by Brett Kail

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 the ServletFileUploadclass. If the class is loaded from anywhere other than WEB-INF/lib, you'll need to remove it.

您必须错误地将 commons-fileupload.jar 复制到JRE/lib/extJRE/lib/endorsed或以其他方式将其放置在对 servlet API 不可见的类路径中。用 启动 JVM -verbose:class,这将打印加载ServletFileUpload类的类路径。如果该类是从 以外的任何地方加载的WEB-INF/lib,则需要将其删除。

回答by Vijay Kumar Kanta

Remove any servlet-api.jar or the upload helping jar commonsfrom JRE/lib or JRE/lib/ext. This helped me fix the problem.

从 JRE/lib 或 JRE/lib/ext 中删除任何 servlet-api.jar 或上传帮助 jar commons。这帮助我解决了问题。

回答by Sandeepraj Singh

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.

旧线程,但仍然可以帮助某人。我看到我在测试范围包含的依赖项中有一个 javax-servlet jar。我做到了提供的范围。如果您使用 Eclipse +Maven,请检查依赖关系图。