NoClassDefFoundError:HSSFWorkbook

时间:2015-11-17 12:53:40

标签: java

我正在尝试使用MapReduce程序处理Excel工作表。我编写了ExcelParser,没有编译时错误。但是当我运行它时会抛出以下错误。

java.lang.Exception: java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
    at org.apache.hadoop.mapred.LocalJobRunner$Job.runTasks(LocalJobRunner.java:462)
    at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:522)
Caused by: java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
    at indiaweather.ExcelParser.parseExcelData(ExcelParser.java:20)
    at indiaweather.ExcelRecordReader.initialize(ExcelRecordReader.java:34)
    at org.apache.hadoop.mapred.MapTask$NewTrackingRecordReader.initialize(MapTask.java:545)
    at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:783)
    at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
    at org.apache.hadoop.mapred.LocalJobRunner$Job$MapTaskRunnable.run(LocalJobRunner.java:243)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.hssf.usermodel.HSSFWorkbook
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 11 more

我添加了所需的jar文件。以下是我的ExcelParser计划。

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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;

public class ExcelParser {

    private static final Log LOG = LogFactory.getLog(ExcelParser.class);
    private StringBuilder currentString = null;
    private long bytesRead = 0;

    public String parseExcelData(InputStream is) {
        try {
            HSSFWorkbook workbook = new HSSFWorkbook(is);

            // Taking first sheet from the workbook
            HSSFSheet sheet = workbook.getSheetAt(0);

            // Iterate through each rows from first sheet
            Iterator<Row> rowIterator = sheet.rowIterator();
            currentString = new StringBuilder();
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();

                // For each row, iterate through each columns
                Iterator<Cell> cellIterator = row.cellIterator();

                while (cellIterator.hasNext()) {

                    Cell cell = cellIterator.next();

                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_BOOLEAN:
                        bytesRead++;
                        currentString.append(cell.getBooleanCellValue() + "\t");
                        break;

                    case Cell.CELL_TYPE_NUMERIC:
                        bytesRead++;
                        currentString.append(cell.getNumericCellValue() + "\t");
                        break;

                    case Cell.CELL_TYPE_STRING:
                        bytesRead++;
                        currentString.append(cell.getStringCellValue() + "\t");
                        break;

                    }
                }
                currentString.append("\n");
            }
            is.close();
        } catch (IOException e) {
            LOG.error("IO Exception : File not found " + e);
        }
        return currentString.toString();

    }

    public long getBytesRead() {
        return bytesRead;
    }

}

我在Buildpath中添加了poi-3.13 jar。我在谷歌搜索但没有解决方案无法解决问题。我也试过添加旧版本的jar文件。但没有任何帮助。

2 个答案:

答案 0 :(得分:0)

您可能已将jar添加到构建路径,这会使代码编译,但它也需要在Java类路径上才能运行它。

如果您从命令行运行它,请使用带有-classpath命令的java开关添加jar。您还可以将其添加到系统上的CLASSPATH环境变量中。

答案 1 :(得分:0)

我认为您需要另外2个jar文件... poi-ooxml.jarpoi-ooxml-schemas-jar。希望这会有所帮助...