3

I am using apache poi to open an existing excel file.

public static int generateReport(Calendar csdate,Calendar cedate) throws Exception
{
     FileInputStream fileIn =null;
     FileOutputStream fileOut = null;
     int sum=0;//For calculating the total number of tickets
    final Workbook wb;
    fileIn =new FileInputStream("d:\\excelfiles\\TicketsReport.xlsm");
    wb = org.apache.poi.ss.usermodel.WorkbookFactory.create(fileIn);<-- Exception
    final Sheet sheet = wb.getSheet("Report");
    //rest of stuff
}

I have a servlet which takes the dates from an html page and call generateReport(). The problem is this program was running previously with same tomcat 6 version and servlet2.4 version.In eclipse there is no compile time error at the WorkbookFactory.create() line. Here's the stack trace.

java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/WorkbookFactory
at ReportFromJira.generateReport(ReportFromJira.java:59)
at JiraReportServlet.doPost(JiraReportServlet.java:52)
at JiraReportServlet.doGet(JiraReportServlet.java:36)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
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.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:128)
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:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)

When I manually explored the jar files I found out that.

poi-3.8-20120326.jar contains
org.apache.poi.ss.usermodel->Workbook.class file

and poi-ooxml-3.8-20120326.jar contains
org.apache.poi.ss.usermodel->WorkbookFactory.class file

I have both the jar files included in my class path.

any idea why the exception?

Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40
  • 2
    it seems that you are mistaken about having them in ur classpath, they have to be in ur tomcat classpath and not just eclipse classpath. Suggest putting one of the jars in WEB-INF/lib manually. – MozenRath Dec 05 '12 at 05:13
  • 1
    See [other](http://stackoverflow.com/questions/779650/where-on-the-file-system-was-my-java-class-loaded-from) [questions](http://stackoverflow.com/questions/947182/how-to-explore-which-classes-are-loaded-from-which-jars) on SO regarding how to debug class loading. – Mr Moose Dec 05 '12 at 05:16
  • 1
    @MozenRath : I think you are right post an answer so that I can accept it – Bhavik Shah Dec 05 '12 at 13:08

3 Answers3

2

org.apache.poi.ss.usermodel.WorkbookFactory is contained with the poi-ooxml jar file. In order to use it, you need the core POI jar, the POI OOXML jar, and their dependencies. The exception you're getting indicates you're missing at least one of those.

You can find out more about the dependencies of the different POI components, both internal and external, on the Apache POI Components page. I'd suggest you review that, review your application and build, then drop in the jars you're missing!

Gagravarr
  • 47,320
  • 10
  • 111
  • 156
  • 1
    And use maven or ivy of gradle or some other build tool that manages dependencies. – bmargulies Dec 05 '12 at 12:29
  • Apache POI is fully available in Maven Central, along with all the dependencies on it. Switching to maven/ivy/gradle is sensible long term, but may not be a pre-req to solving this one issue! – Gagravarr Dec 05 '12 at 12:30
  • I have both the jar files included. it's mentioned in the question – Bhavik Shah Dec 05 '12 at 13:07
  • Nope, you *think* you have included the jars, but you clearly haven't hence the error... You need to make sure the jars (and their dependencies!) are available both when compiling *and* when running the code – Gagravarr Dec 05 '12 at 14:04
  • @Gagravarr : k now i'm sure I have both the jar files included – Bhavik Shah Dec 06 '12 at 04:54
0

Just put one of the jars in the WEB-INF\lib of your WAR\EAR because that is where Tomcat Picks the jars from by default.

Just including in eclipse project is not enough when tomcat is not being run though eclipse configuration.

MozenRath
  • 9,652
  • 13
  • 61
  • 104
0

The solution for this is very simple :

  1. download all the jar file in this link http://archive.apache.org/dist/poi/release/bin/poi-bin-3.9-20121203.zip
  2. extract the file and copy all the jar file to "\WebContent\WEB-INF\lib\" in your project. PLEASE NOTE : YOU MUST COPY AND PASTE IT RIGHT INTO YOUR PROJECT IN ECLIPSE, don't copy paste it through windows explorer or place it as external JAR in java build path, because it will never works.

that's it, you're done.. :D Please let me know if this solve your problem..