Tag Archives: NoClassDefFound

Web: xssfworkbook error NoClassDefFoundError [How to Solve]

I. background introduction

The Java Web environment needs to read the data in the excel table and insert it into the database. The xssfworkbook class needs to be used to read the table file. The key codes are as follows:

  //read excel
    public static Workbook readExcel(String filePath) {
        Workbook wb = null;
        if (filePath == null) {
            return null;
        }
        String extString = filePath.substring(filePath.lastIndexOf("."));
        InputStream is = null;
        try {
            is = new FileInputStream(filePath);
            if (".xls".equals(extString)) {
                return wb = new HSSFWorkbook(is);
            } else if (".xlsx".equals(extString)) {
                return wb = new XSSFWorkbook(is);
            } else {
                return wb = null;
            }

        } catch (FileNotFoundException e) {
            LOGGER.error(e.toString());
        } catch (IOException e) {
            LOGGER.error(e.toString());
        } 
        return wb;
    }

II. Occurrence of problems

Running locally, such as writing a main method or running with @ test, can run normally, but on the web, the interruption point finds that this class cannot be found
so it tries to upgrade the dependency package

or not. Unable to solve it, he fell into deep thinking. It’s really hard to deal with environmental problems and dependency problems
tried the recommended dependencies and versions given by other authors, but still failed:
First:

Second:

Three problem solving

Bypass the problem. If the mountain doesn’t come, I’ll go
change the file format: that is, xlsx is XLS, and finally return to the hssfworkbook class for reading.

PATH = "/Users/xxx/work/B/A_info.xlsx";

by

PATH = "/Users/xxx/work/B/A_info.xls";

So you can access it in the web layer
(PS: it should be noted that if you directly change the. Xlsx format file to. XLS, you need to open the file first and click Save as. XLS format. You can’t be lazy here. The author has also stepped on this pit)