Erfan Bashar
Erfan Bashar

Reputation: 21

Getting java.lang.NoClassDefFoundError error when writing Excel file

I will be developing a workflow where I would be needed to fetch some data from Oracle DB and write the ResultSet in Excel file. Before starting that, I would like to do some tests. However, I am getting the following runtime exception java.lang.NoClassDefFoundError. Details can be found below. I know I am missing something very silly but I am struggling to find that out. I appreciate your help.

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap
    at DatabaseConnPkg.DatabaseConnClass.main(DatabaseConnClass.java:32)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections4.ListValuedMap
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more
C:\Users\Erfan\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 3 seconds)

These are the external jars that I am using

poi-4.1.2.jar

poi-ooxml-4.1.2.jar

This is the code that I am working with

Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    ResultSetMetaData resultSetMetaData = null;

    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","hr","hr");
        String sql = "select * from countries";
        preparedStatement = connection.prepareStatement(sql);
        resultSet = preparedStatement.executeQuery();
        resultSetMetaData = resultSet.getMetaData();
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet spreadSheet = workbook.createSheet();
        XSSFRow row = spreadSheet.createRow(1);
        XSSFCell cell;

        for(int i=0; i<resultSetMetaData.getColumnCount(); i++) {
            cell = row.createCell(i);
            cell.setCellValue(resultSetMetaData.getColumnName(i));
        }

        while(resultSet.next()) {
            row = spreadSheet.createRow(1);
            cell = row.createCell(1);
            cell.setCellValue(resultSet.getString(1));
            cell = row.createCell(2);
            cell.setCellValue(resultSet.getString(2));
            cell = row.createCell(3);
            cell.setCellValue(resultSet.getString(3));

        }

        FileOutputStream fileOutputStream = new FileOutputStream(new File(FILE));
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        System.out.println("exceldatabase.xlsx written successfully");

        //System.out.println(resultSetMetaData.getColumnName(1) + "\t" + resultSetMetaData.getColumnName(2) + "\t" + resultSetMetaData.getColumnName(3));

        /*
        while(resultSet.next()) {
            System.out.println(resultSet.getString(1) + "\t\t" + resultSet.getString(2) + "\t\t" + resultSet.getString(3));
        }
        */

        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

Upvotes: 0

Views: 696

Answers (1)

petrubear
petrubear

Reputation: 721

It seems that you are missing the commons-collections4 dependency, you can get it from here

Upvotes: 2

Related Questions