3

while uploading .xlsx java.lang.NoClassDefFoundError: Could not initialize class org.apache.poi.poifs.filesystem.FileMagic and while uploading .xls file getting error java.io.IOException: Unable to read entire header; 0 bytes read; expected 512 bytes

In am creating a web application and in that I am uploading an excel and fetching its data to add in mysql database by using row and cell index numbers it is working fine, but after uploading web application to server it showing error while i am uploading excel to fetch data and adding those data to mysql database. my code is on link


below is the error: while uploading .xlsx file


Type Exception Report

Message Servlet execution threw an exception

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: Servlet execution threw an exception org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) Root Cause

java.lang.NoClassDefFoundError: Could not initialize class org.apache.poi.poifs.filesystem.FileMagic org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:208) org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:98) org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:324) org.apache.poi.util.PackageHelper.open(PackageHelper.java:37) org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:295) com.filetwo.doPost(filetwo.java:131) javax.servlet.http.HttpServlet.service(HttpServlet.java:661) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) Note The full stack trace of the root cause is available in the server logs.


but when i am uploading .xls file i am getting error I am getting following error


HTTP Status 500 ? Internal Server Error Type Exception Report

Message Unable to read entire header; 0 bytes read; expected 512 bytes

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

java.io.IOException: Unable to read entire header; 0 bytes read; expected 512 bytes org.apache.poi.poifs.storage.HeaderBlock.alertShortRead(HeaderBlock.java:226) org.apache.poi.poifs.storage.HeaderBlock.readFirst512(HeaderBlock.java:207) org.apache.poi.poifs.storage.HeaderBlock.(HeaderBlock.java:104) org.apache.poi.poifs.filesystem.POIFSFileSystem.(POIFSFileSystem.java:138) org.apache.poi.hssf.usermodel.HSSFWorkbook.(HSSFWorkbook.java:322) org.apache.poi.hssf.usermodel.HSSFWorkbook.(HSSFWorkbook.java:303) com.filetwo.doPost(filetwo.java:542) javax.servlet.http.HttpServlet.service(HttpServlet.java:661) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) Note The full stack trace of the root cause is available in the server logs.

smita mishra
  • 41
  • 1
  • 6
  • 1
    Possible duplicate of [Why am I getting a NoClassDefFoundError in Java?](https://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java) – Kaustubh Khare Apr 11 '18 at 09:29
  • Hello, are you able to find soluction for this error ? – Maher Feb 24 '21 at 14:36

1 Answers1

2

It seems that your local/dev environment isn't in sync with you server environment.

The error says that the class your program depends on wasn't found on the classpath. As this worked on your local/dev, ensure that the server environment has the same set of jar that your local/dev environment has.

scourer
  • 49
  • 6
  • but the files are present in web-INF/lib folder – smita mishra Apr 11 '18 at 11:03
  • can you please post some code snippet and complete exception stacktrace? – scourer Apr 11 '18 at 15:59
  • Sorry to say you are making it harder to help you. Your code and stacktrace in your question don't match. Please be specific with the details you provide. As I have already mentioned some jar or their versions are mismatching in the two environment. – scourer Apr 12 '18 at 10:14