0

I am getting the below error when trying to read an Excel file :

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
    at 
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 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more

I have used the below code to read Excel file with an extension of .xls. I have tried searching for answers by importing other poi jars but it didn't help, I have checked searching the web but again it didn't help.

public Object[][] getExcelData(String excelLocation, String sheetName) {
        try {
            Object dataSets[][] = null;
            FileInputStream file = new FileInputStream(new File(excelLocation));
HSSFWorkbook workbook = new HSSFWorkbook(excelLocation);
HSSFSheet sheet = workbook.getSheet(sheetName);
int totalRowNum = sheet.getLastRowNum();
int totalColumnNum = sheet.getRow(0).getLastCellNum();
dataSets = new Object[totalRowNum][totalColumnNum];
Iterator<Row> rowIterator = sheet.iterator();
            int i = 0;
while (rowIterator.hasNext()) {
                i++;
HSSFRow row = (HSSFRow) rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();
                int j = 0;
                while (cellIterator.hasNext()) {
                    j++;
                    HSSFCell cell = (HSSFCell) cellIterator.next();
                    switch (cell.getCellTypeEnum()) {
                    case STRING:
                    dataSets[i][j] = cell.getStringCellValue();
                        break;
                    case NUMERIC:
                        dataSets[i][j] = cell.getNumericCellValue();
                        break;
                    case BOOLEAN:
                        dataSets[i][j] = cell.getBooleanCellValue();
                        break;
                    case FORMULA:
                        dataSets[i][j] = cell.getCellFormula();
                        break;
                        default:
                        log.info("No matching ENUM Type Found");
                        break;
                   }
                }
            }
          return dataSets;
        } catch (Exception e) {
            log.info(e.getCause());
            e.printStackTrace();
        }
        return null;
    }
public static void main(String[] args) {

        ExcelHelper excel = new ExcelHelper();
        String excelLocation = ResourceHelper.getResourcePath("\\src\\main\\resources\\testData\\testData.xls");
        Object[][] data = excel.getExcelData(excelLocation, "Login");

    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Akash
  • 115
  • 2
  • 13
  • 1
    I doubt anyone will click a random external URL. – achAmháin Oct 30 '18 at 11:15
  • Possible duplicate of [java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook](https://stackoverflow.com/questions/6464919/java-lang-noclassdeffounderror-org-apache-poi-hssf-usermodel-hssfworkbook) – achAmháin Oct 30 '18 at 11:16
  • What to do it is not allowing me to put the external code here it is saying too much code and i don't know how to edit the same so that it favors the rule set by stack overflow @achAmhain – Akash Oct 30 '18 at 11:18
  • Yes it is a duplicate but on the given question link i have tried all the methods none of them has worked that's why i have opened up a new question @achAmháin – Akash Oct 30 '18 at 11:19
  • If it says too much code, then short down the neccessary code we need to see in order to understand your problem. – L. Guthardt Oct 30 '18 at 11:20
  • @L.Guthardt the code is available on the below url along with pom.xml file:https://docs.google.com/document/d/1-IFa7Ml9nMiaVzlecsCr6haPmqlrmMp7YNHg5wPvVMs/edit – Akash Oct 30 '18 at 11:22
  • 2
    @Akash No I don't click your external link, sorry. Take some time, shorten the code to the only code we need to understand your problem and then share only this code. We won't go through 10.000 lines of code. Put some effort into your question, since you are the one that wants our help. – L. Guthardt Oct 30 '18 at 11:24
  • @L.Guthardt : Now can you check and help me – Akash Oct 30 '18 at 11:30
  • @Akash Come on Akash, don't make it so hard, please. I mean look at your code. It's just one big bunch of messy code, just format it properly like every normal programmer does. I mean how should I be able to look at the unstructured code and understand it. – L. Guthardt Oct 30 '18 at 11:32
  • @Akash Moreover where exactly do you get your exeption? – L. Guthardt Oct 30 '18 at 11:34
  • @L.Guthardt: Getting error at below line: HSSFWorkbook workbook = new HSSFWorkbook(excelLocation); – Akash Oct 30 '18 at 11:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/182788/discussion-between-akash-and-l-guthardt). – Akash Oct 30 '18 at 11:39

1 Answers1

1

Remove .m2 from your user location-> do Maven force update ->Run