Home » excel » java – Could not initialize class org.apache.poi.poifs.filesystem.FileMagic

java – Could not initialize class org.apache.poi.poifs.filesystem.FileMagic

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&Answers:

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.