Results 1 to 2 of 2
  1. #1
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    106
    Rep Power
    0

    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:

    Java 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:
    Java 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)
    Java 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
    XML 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:
    Java 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??
    Last edited by vector_ever; 07-18-2013 at 08:17 PM.

  2. #2
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    106
    Rep Power
    0

    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:

    XML 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
    XML 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. Replies: 1
    Last Post: 08-24-2012, 09:49 AM
  2. problem in reading data from excel sheet
    By jainmayank33 in forum Advanced Java
    Replies: 2
    Last Post: 11-03-2011, 03:05 AM
  3. Replies: 5
    Last Post: 11-22-2010, 07:33 PM
  4. How to add data into excel file
    By computechsolutions in forum JDBC
    Replies: 1
    Last Post: 10-04-2010, 12:29 PM
  5. To populate the data processed into a .csv or excel file
    By jaiminparikh in forum Advanced Java
    Replies: 2
    Last Post: 02-26-2009, 11:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •