0

Here is my servlet class

package com.zaggle; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class FirstServlet */ public class FirstServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public FirstServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String[] data;String val=null,name; name=request.getParameter("excelsheet"); System.out.println(name); response.setContentType("text/html"); UploadExcel ex= new UploadExcel(); data=ex.procedure(name); System.out.println(name); request.setAttribute("val", data); System.out.println(val); String destination="/NewFile1.jsp"; RequestDispatcher rd=getServletContext().getRequestDispatcher(destination); //doGet(request, response); rd.forward(request, response); } } 

Here is my normal java class which I am using in servlet class

package com.zaggle; import java.io.FileInputStream; import java.io.IOException; import java.util.Iterator; import java.util.Scanner; import java.util.Vector; import javax.swing.plaf.basic.BasicInternalFrameTitlePane.RestoreAction; import org.apache.poi.hssf.record.formula.functions.Goto; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class UploadExcel { public String[] procedure(String filename) { // Scanner scanner = new Scanner(System.in); //System.out.println("Please enter a filename with extention to upload"); String fileName = "C:\\"+filename;//+scanner.nextLine(); String[] dataHolder = ReadCSV(fileName); // printCellDataToConsole(dataHolder); return dataHolder; } public static void printCellDataToConsole(Vector dataHolder) { // TODO Auto-generated method stub for(int i=0;i<dataHolder.size();i++) { Vector column=(Vector)dataHolder.elementAt(i); for (int j = 0; j < column.size(); j++) { HSSFCell myCell = (HSSFCell) column.elementAt(j); String stringCellValue = myCell.toString(); System.out.print(stringCellValue + "\t"); } System.out.println(); } } public static String[] ReadCSV(String fileName) { // TODO Auto-generated method stub Vector cellVectorHolder = new Vector(); String[] col = null; try { FileInputStream myInput = new FileInputStream(fileName); POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput); HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem); HSSFSheet mySheet = myWorkBook.getSheetAt(0); Iterator rowIter = mySheet.rowIterator(); while (rowIter.hasNext()) { HSSFRow myRow = (HSSFRow) rowIter.next(); Iterator cellIter = myRow.cellIterator(); Vector column = new Vector(); while (cellIter.hasNext()) { HSSFCell myCell = (HSSFCell) cellIter.next(); column.addElement(myCell); } col=column.toString().split(" "); cellVectorHolder.addElement(column); } } catch(IOException ie) { System.err.println("Please enter a valid input"); } catch (Exception e) { e.printStackTrace(); } //return cellVectorHolder; return col; } } 

When i run I am getting the error like this

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet execution threw an exception

root cause

java.lang.NoClassDefFoundError: org/apache/poi/poifs/filesystem/POIFSFileSystem com.zaggle.UploadExcel.ReadCSV(UploadExcel.java:52) com.zaggle.UploadExcel.procedure(UploadExcel.java:24) com.zaggle.FirstServlet.doPost(FirstServlet.java:42) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 

Why am I getting this error. Can anyone help me?

5
  • Well where are all the Apache POI libraries at execution time? Commented Oct 17, 2012 at 6:51
  • Looks like your missing some Apache POI libraries in your project or at least they're missing when you deploy the web app. Commented Oct 17, 2012 at 6:51
  • @ Jon Skeet @ Luggi Mendoza I have added it in java Reference Libraries Commented Oct 17, 2012 at 6:53
  • Check that the Apache POI libraries deployed in your app contains the org.apache.poi.poifs.filesystem.POIFSFileSystem class. Commented Oct 17, 2012 at 6:55
  • 1
    @Rag : You have library in class path but you need to put it in your project's WEB-INF/lib Commented Oct 17, 2012 at 6:55

4 Answers 4

3

It looks like you've forgotten to include the POI library on the CLASSPATH (e.g. It's not referenced by your web app). You can include this lib under WEB-INF/lib (in your WAR archive or in the exploded directory format) or it can sit elsewhere in your app server/web server CLASSPATH.

Sign up to request clarification or add additional context in comments.

Comments

1

you are getting the exception in this line

POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput); 

You are trying to use a library that is not included in your project. Make sure that import org.apache.poi library in in the web-inf/lib directory

Comments

1

Your POI jars are not in the classpath.You can include this lib under WEB-INF/lib..

Also check could be that your version of jars is different than those expected.

Comments

1

Make sure apache-poi.jar under WEB-INF/lib.

Optional : Based on the your web server or application server, put apache-poi.jar into server-home/lib(depend on server). It is not best solution.


Note

Now, you are developing web application, don't use uploaded file with specific path (eg: C:\....). If so, the program will find out the upload file on local machine Web Server is running. First you have to upload file as byte[] or InputStream or use other third party lib. After that, you have to change HSSFWorkbook.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.