السلام عليكم
لدي مشكلة ليست بالهينة ولا اعرف ان كنت قد أجد المساعدة لديكم حيث يكمن الهدف النهائي في عرض نتائج وفق Json Format عن طريق Jeresy التي عن طريقها أستطيع بناء صفحة ويب على اساس Restful Service
المشكلة باختصار انه لدي ملف اكسل احاول استخلاص بعض البيانات منه وتخزينها في Array List ومن ثم اقوم باستخدام هذه الـ Array List في كود Jeresy من اجل أن اخرج هذه البيانات على هيئة Json Format
ولدى تشغيلي كود الـ Jeresy يتم تشغيل سيرفر التومكات وثم أحصل على الخطأ التالي:
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الآن سأقوم بعرض الأكواد المستخدمة في المشروع
أولا الكود المستخدم لقراءة البيانات واستخلاصها من ملف اكسل وتخزينها في Array List
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();
}
}
}ثانيا كود 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();
}
}ملف الـ web.xml
<?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>ملاحظة: لدى محاولة حقن ملف Jeresy - Restful بـ Array List من صمف مستقل وليس من ملف اكسل كما يلي مثلاً:
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);
}
}
}فان البرنامج يشتغل تمام ولا يحدث ان خطأ
من الواضح لدي ان المشكلة تحدث فقط لدى التعامل مع ملف الاكسل او الكود المسؤول عن التعامل مع ملف الاكسل
قد احاولت ان أجد حلأ التوائيا بان اقوم بنسخ Array List الى Array List أخرى في صنف مستقل والتعامل مع هذا الصنف وهذا الـ Array List داخل كود الـ jeresy لكن هذا أيضا لم ينفع ويتم فورا توليد نفس الخطأ
هل لدى أي احد من الاخوة أي معلومة او فكرة
ملفات الجار في المرفقات