Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problem obtain data from excel file via Jersy

  1. #1
    Member
    Join Date
    Jul 2013
    Posts
    46
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Problem obtain data from excel file via Jersy

    Hello,

    my objective is to build a Restful Service, so i use "Jeresy" (javax.ws.rs) to make it, now i try to obtain data from excel file an put them in array list, then i will use those data (which stored in the array list) in the code of Jersy to display it in Json format, but i get always error as the flowing:


    HTTP Status 500 - java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
     
    type Exception report
     
    message java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
     
    description The server encountered an internal error that prevented it from fulfilling this request.
     
    exception
     
    javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
        com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:420)
        com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
        com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    root cause
     
    java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
        JavaBeans.Reader.read(Reader.java:30)
        JavaBeans.ArrList.arrList(ArrList.java:18)
        com.crunchify.restjersey.RestfullJersy.convertFtoC(RestfullJersy.java:21)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        java.lang.reflect.Method.invoke(Unknown Source)
        com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
        com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
        com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
        com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
        com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
        com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
        com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
        com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1480)
        com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1411)
        com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1360)
        com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1350)
        com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
        com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
        com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    note The full stack trace of the root cause is available in the Apache Tomcat/7.0.41 logs.
     
    Apache Tomcat/7.0.41

    The codes:
    First code of Apache POI to read from excel file:

    package JavaBeans;
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Iterator;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.json.simple.JSONObject;
     
     
     
    public class Reader {
     
        protected static ArrayList col = new ArrayList();
     
     
     
            public void read(){
     
        try {
     
            FileInputStream file = new FileInputStream(new File("d:\\hi.xls"));
     
            //Get the workbook instance for XLS file 
            HSSFWorkbook workbook = new HSSFWorkbook(file);
     
            //Get first sheet from the workbook
            HSSFSheet sheet = workbook.getSheetAt(0);
     
            //Iterate through each rows from first sheet
            Iterator<Row> rowIterator = sheet.iterator();
            while(rowIterator.hasNext()) {
                Row row = rowIterator.next();
     
                //display from the third row until 5th
                if(row.getRowNum()>2 && (row.getRowNum()<5))
                {
                {
     
                //For each row, iterate through each columns
                Iterator<Cell> cellIterator = row.cellIterator();
                while(cellIterator.hasNext()) {
     
                    //Getting the cell contents
                    Cell cell = cellIterator.next();
     
                    switch(cell.getCellType()) {
                        case Cell.CELL_TYPE_BOOLEAN:
                            System.out.print(cell.getBooleanCellValue() + "\t\t");
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "\t\t");
                            break;
                        case Cell.CELL_TYPE_STRING:
                            System.out.print(cell.getStringCellValue() + "\t\t");
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            System.out.println(cell.getCellFormula());
                            break;
     
                       /** case Cell.CELL_TYPE_BLANK:
                            System.out.println("BLANK");
                            break;
                            **/
                    }
     
                }
     
                }
                }
                //store the values of the third Column
                 Cell cell = row.getCell(2); //if (cell.getColumnIndex() == 2)
                       if(cell != null){
                    //add the values of the cell to the Arraylist 
                    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) 
                    {
                    col.add(cell.getNumericCellValue());
                    } 
                    else if (cell.getCellType() == Cell.CELL_TYPE_STRING) 
                    {
                    col.add(cell.getRichStringCellValue().getString());
                    } 
                    else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) 
                    {
                    col.add(cell.getBooleanCellValue());
                    }
                    }
                System.out.println("");
     
            }
     
            file.close();
     
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        }

    second the code of the Jeresy (Restful)

    package com.crunchify.restjersey;
     
     
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.Response;
    import org.json.JSONException;
    import org.json.JSONObject;
    import JavaBeans.Reader;
     
    @Path("/Exceltojersy")
    public class RestfullJersy extends Reader{
        @GET
        @Produces("application/json")
        public Response convertFtoC() throws JSONException {
     
            Reader read = new Reader();
            read.read();
     
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("coloum 0", col.get(0)); 
                    jsonObject.put("coloum 1", col.get(1)); 
                    jsonObject.put("coloum 2", col.get(2)); 
                    jsonObject.put("coloum 3", col.get(3));
                    jsonObject.put("coloum 4", col.get(4));
                    //jsonObject.put("coloum 5", col1.get(5)); 
     
                    String result = "@Produces(\"application/json\") Output: \n\nArrayList Output: \n\n" + jsonObject;
                    return Response.status(200).entity(result).build();
        }
     
    }
    PS: ithe Jeresy code work very well if it tried to get the data from arraylist in independent class where no "Poi" to read excel file

    for example:
    package JavaBeans;
     
    import java.util.ArrayList;
     
    public class ArrList1 {
     
    protected static ArrayList col1 = new ArrayList();
     
     
        public  void arrList() {
            col1.add(1);
            col1.add(2);
            col1.add(3);
            col1.add(4);
            col1.add(5);
            col1.add(6);
     
            //print the value of the cells which is stored in the the Arraylist
            System.out.println("");
            for (int i = 0; i < col1.size(); i++){
            Object item = col1.get(i);
            System.out.println("New Coloum " + i + " : " + item);
                }
        }
    }
    heir if i injected this arraylist in the jeresy code it will works, but i need to get the data from Excel file

    So any idea??


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    46
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem obtain data from excel file via Jersy

    ok that was mein Failure, i forgot to add the Libraries to the Directory /WebContent/WEB-INF/lib/ of the Projekts
    now i did it and the past error show up never any more, but another type of the error:

    HTML Code:
    HTTP Status 500 - Servlet.init() for servlet Jersey Web Application threw exception
    
    type Exception report
    
    message Servlet.init() for servlet Jersey Web Application threw exception
    
    description The server encountered an internal error that prevented it from fulfilling this request.
    
    exception
    
    javax.servlet.ServletException: Servlet.init() for servlet Jersey Web Application threw exception
    	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    	org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    	org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    	org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    	org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    	java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    	java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    	java.lang.Thread.run(Unknown Source)
    root cause
    
    com.sun.jersey.spi.inject.Errors$ErrorMessagesException
    	com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
    	com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
    	com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
    	com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:765)
    	com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:760)
    	com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:489)
    	com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:319)
    	com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
    	com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
    	com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:374)
    	com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:557)
    	javax.servlet.GenericServlet.init(GenericServlet.java:160)
    	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    	org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    	org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    	org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    	org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    	java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    	java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    	java.lang.Thread.run(Unknown Source)
    note The full stack trace of the root cause is available in the Apache Tomcat/7.0.41 logs.
    where the servlet Jersey Web Application is the name of the servelet im my web.xml
    HTML Code:
    <servlet-name>Jersey Web Application</servlet-name>
    i thing ithis error depends on the web.xml
    but i do'nt know how to solve it

Similar Threads

  1. how to export data from servlet to excel file through poi lib
    By nicholas.omosa in forum Java Servlet
    Replies: 0
    Last Post: November 10th, 2012, 09:13 PM
  2. reporting to excel file using servlet and jsp problem
    By nicholas.omosa in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 10th, 2012, 08:50 PM
  3. Replies: 3
    Last Post: September 1st, 2011, 10:49 AM
  4. Problem extracting data from file
    By hello_world in forum File I/O & Other I/O Streams
    Replies: 17
    Last Post: August 21st, 2011, 09:35 PM
  5. Facing problem with posting Excel file for download - Content in browser
    By ragz_82 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: February 9th, 2010, 08:28 AM