I use Tomcat. When calling the post-method, an error occurs:

java.lang.NoClassDefFoundError: javax / servlet / http / HttpServletRequest org. .HttpServlet.service (HttpServlet.java:648) javax.servlet.http.HttpServlet.service (HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.websocket.server.

When you call another post-method, errors do not occur, but when you try to implement the ability to upload files to the server using a servlet:

package controller; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Iterator; import java.util.List; /** * Created by Игорь on 10.05.2016. */ public class PDFUpload extends HttpServlet { private String filePath; boolean isMultipart; @Override public void init() { // Get the file location where it would be stored Path path = Paths.get("files/file.txt"); try { Files.createDirectories(path.getParent()); } catch(IOException e) { e.printStackTrace(); } filePath = path.toString(); } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Check that we have upload request isMultipart = ServletFileUpload.isMultipartContent(request); response.setContentType("text/html"); PrintWriter out = response.getWriter(); if(!isMultipart) { out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>No file uploaded</p>"); out.println("</body>"); out.println("</html>"); return; } // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(); // Maximum file size to be uploaded upload.setSizeMax(10000 * 1024); try { // Parse the request to get file items List<FileItem> fileItems = upload.parseRequest(request); // Process the uploaded file items Iterator i = fileItems.iterator(); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); while(i.hasNext()) { FileItem fi = (FileItem)i.next(); if(!fi.isFormField()) { out.println(fi.getName()); } } out.println("</body>"); out.println("</html>"); } catch (Exception e) { e.printStackTrace(); } } } 

The error above. To upload files I use jar: commons-io-2.5 ​​and commons-fileupload-1.3.1. I add it to the project in the library and it is in the CLASSPATH environment variable (C: \ Program Files \ Java \ jdk1.8.0_74 \ jre \ lib \ ext \ commons-io-2.5.jar; C: \ Program Files \ Java added to the variable value; \ jdk1.8.0_74 \ jre \ lib \ ext \ commons-fileupload-1.3.1.jar).

What is the problem?

  • The web application has its own idea of ​​the classpath. The library should be in the WEB-INF / lib folder of the war archive or war folder. Or among the common libraries of the application server. I do not know what you are collecting your project, but it should be possible to drop the necessary libraries into WEB-INF / lib. Also, do not forget about the dependencies of the library itself, they must also be added to WEB-INF / lib or to the server’s shared libraries - Sergey
  • For the project I use IntelliJ IDEA - Igor Gorbunov

1 answer 1

Maybe someone will also face this problem! The point is that I dropped commons-io-2.5.jar and commons-fileupload-1.3.1.jar to drive C, and Windows Firewall blocked access to jar files during program execution, although the classes were visible before compilation. Everything was decided after moving the files to another location. Here is a useful link:

http://javarevisited.blogspot.com/2011/06/noclassdeffounderror-exception-in.html