java.lang.ClassNotFoundException: org.apache.xml.serializer.OutputPropertiesFactory is thrown after upgrading xalan to 2.7.3
Asked Answered
A

1

11

I have java code that exports some data into an excel file. ( I only included below what I think are the relevant parts in the code)

Everything worked fine but then I upgraded my xalan from 2.7.2 to 2.7.3 and it stoped working. the reason - java.lang.ClassNotFoundException: org.apache.xml.serializer.OutputPropertiesFactory

The exception is thrown at the 'wb.write(fileOutputStream);' line under the export() method

import org.apache.logging.log4j.Level;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
     
private Workbook wb;

            private void init(String targetPath, String fileName, ExcelExportType exportType) {
                exportedFile = new File(Paths.get(targetPath, fileName  + FILE_EXTENSION).toString());
                try {
                    fileOutputStream = new FileOutputStream(exportedFile);
                    // if file doesn't exists, then create it
                    if (!exportedFile.exists()) {
                        exportedFile.createNewFile();
                    }
                } catch (Exception e) {
                    throw new RuntimeException("Init failed. File " + exportedFile.getName() + " cannot be found");
                }
        
                switch (exportType) {
                    case MS_EXCEL_2007_AND_UP_STREAMING:
                        wb = new SXSSFWorkbook();
                        break;
                    default:
                        throw new RuntimeException("Unsupported export type");
                }
        
                wb.setMissingCellPolicy(Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                creationHelper = wb.getCreationHelper();
                sheet = wb.createSheet(getSheetName());
                Font font = wb.createFont();
                font.setFontName(DEFAULT_FONT_NAME);
        
                if(sheet instanceof SXSSFSheet) {
                    ((SXSSFSheet) sheet).trackAllColumnsForAutoSizing();
                }
        
                format = wb.createDataFormat();
        
                headerFont = wb.createFont();
                headerFont.setFontName(DEFAULT_FONT_NAME);
                headerFont.setBold(true);
        
                cellFont = wb.createFont();
                cellFont.setFontName(DEFAULT_FONT_NAME);
        
            }
        
        public File export() {
            double factor = 390;
            for (int i = 0; i < numOfColumns; i++) {
                sheet.autoSizeColumn(i);
                sheet.setColumnWidth(i, getWidth((int)(maxColumnWidth.get(i) * factor)));
            }
        
            sheet.createFreezePane(0,1);
        
            try {
                wb.write(fileOutputStream);
                fileOutputStream.close();
                wb.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        
            return exportedFile;
        }

according to the link, https://xalan.apache.org/xalan-j/

Xalan-Java Version 2.7.3 works with Xerces-Java, and the distribution includes xercesImpl.jar from Xerces-Java 2.12.2.

The Xalan-Java implementation is in xalan.jar and serializer.jar. The SAX, DOM, and JAXP 1.3 interfaces are in xml-apis.jar.

so maybe I need to implement the code in a different way? any idea how can I solve this?

Aksoyn answered 26/6, 2023 at 10:20 Comment(0)
A
22

fixed by adding this to my maven dependencies

    <dependency>
        <groupId>xalan</groupId>
        <artifactId>serializer</artifactId>
        <version>2.7.3</version>
    </dependency>
Aksoyn answered 26/6, 2023 at 11:19 Comment(2)
You can also accept your own answer to indicate that the question is solved.Bylaw
I had java.lang.NoClassDefFoundError: org/apache/xml/serializer/SerializerTrace, but solution is the same, thanks for sharing!Melvin

© 2022 - 2024 — McMap. All rights reserved.