'Unable to read Excel using Apache POI

I'm reading excel using Apache POI but it keeps giving error for XSSFWorkbook class definition found error. I used different versions of Apache-poi jar libraries (i.e 4.1 , 4.0 and 3.12) none of them seem to fix this error. Here's a screenshot of libraries currently imported enter image description here Whats wrong inside the code ?

try {   
        File fileSrc = new File("C://Eclipse-Workspace//TestData//TestDataSet.xlsx");
            FileInputStream fis = new FileInputStream(fileSrc); 
            XSSFWorkbook workbook = new XSSFWorkbook(fis);
            XSSFSheet sheet = workbook.getSheetAt(0);
        XSSFCell cellData = sheet.getRow(rowNo).getCell(colNo);
workbook.close();
return cellData.toString();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

Error shown:

 java.lang.NoClassDefFoundError: 
    org/apache/poi/ss/usermodel/WorkbookFactory
    at Utilities.DataAccessorExcel.excelReader(DataAccessorExcel.java:33)
    at TestCases.LoginTest.verifyLogin(LoginTest.java:66)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native 
    Method)
    atjava.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMe 
    thodAccessorImpl.java:62)
    atjava.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Dele 
    gatingMethodAccessorImpl.java:43)at 
    java.base/java.lang.reflect.Method.invoke(Method.java:567)


Solution 1:[1]

I created the project again, and placed POI libraries under ClassPath, but I found that the POI libraries were added as ModulePath and not under ClassPath. Anyhow,I tried this before, I started creating a new project. And moved testng.xml at src folder level. It's working fine as expected.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1