Quantcast
Channel: Java Programming Forum - Learn Java Programming - Apache POI
Viewing all articles
Browse latest Browse all 120

Problem obtain data from excel file via Jersy

$
0
0
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:

Code:

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 code:
First code of Apache POI to read from excel file:
Code:

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;
                                                                              }
                                        }
                              }
                }
                //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)
Code:

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();
        }
       
}

web.xml file
Code:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/crunchify/*</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>
30
</session-timeout>
</session-config>

</web-app>


PS: ithe Jersy 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:
Code:

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??

Viewing all articles
Browse latest Browse all 120