lundi 2 mars 2015

java.lang.ClassNotFoundException: sun.awt.X11FontManager from intellij using jdk 1.7.0_71


Vote count:

0




I have a code that exports some data to excel file.

I am using HSSFWorkbook and HSSFSheet from poi-3.11-20141221.jar of Apache to create the sheet.

Using java 1.6.30 it works.

Changing to java 1.7 or 1.8 I got the ClassNotFoundException when calling autoSizeColumn() (method of HSSFSheet)

I am using Intellij 13.1.6.


Here is my function:



public static TempFile createExcelFile(String sheetTitle, String title, String headerSummary, List<String> header, List<LinkedHashMap<String, String>> data)
{
TempFile result = new TempFile(FileKeyFactory.getFileKey(ExcelReporter.class).getFullPathKey());
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet spreadSheet;
DataFormat format;
int rowIndex = 0;

// Get a DataFormat object and use it to create a CellStyle object
// with the following format set for the cells @. The @ or ampersand
// sets the format so that the cell will hold text.
format = wb.createDataFormat();

if (sheetTitle != null)
{
spreadSheet = wb.createSheet(sheetTitle);
}
else
{
spreadSheet = wb.createSheet();
}

if (title != null)
{
// Title
HSSFFont titleFont = wb.createFont();
titleFont.setFontHeightInPoints((short) 14);
titleFont.setItalic(true);
HSSFRow titleRow = spreadSheet.createRow(rowIndex++);
HSSFCell cell = titleRow.createCell(0);
cell.setCellValue(title);
cell.getCellStyle().setFont(titleFont);
spreadSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 100));
}
else
{
spreadSheet = wb.createSheet();
}

HSSFCellStyle style = wb.createCellStyle();
style.setDataFormat(format.getFormat("@"));

if (headerSummary != null)
{
HSSFFont headerFont = wb.createFont();
headerFont.setFontHeightInPoints((short) 9);
headerFont.setItalic(false);

HSSFCellStyle headerCellStyle = wb.createCellStyle();
headerCellStyle.setDataFormat(format.getFormat("@"));
headerCellStyle.setFont(headerFont);
headerCellStyle.setWrapText(true);

HSSFRow headerRow = spreadSheet.createRow(rowIndex++);
HSSFCell headerCell = headerRow.createCell(0);

headerCell.setCellValue(headerSummary);
headerCell.setCellStyle(headerCellStyle);
//increase row height to accommodate two lines of text
headerRow.setHeightInPoints((9 * spreadSheet.getDefaultRowHeightInPoints()));

//adjust column width to fit the content
spreadSheet.autoSizeColumn((short) 1);
spreadSheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 100));
}


/*
* Data table header
*/
HSSFRow dateRow = spreadSheet.createRow(rowIndex++);
Calendar calendar = Calendar.getInstance();
HSSFCreationHelper createHelper = wb.getCreationHelper();
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
HSSFCell cell = dateRow.createCell(0);
cell.setCellValue("Created: " + DateParser.toString(calendar, DateParser.MONTH_WORD_DATE_FORMAT));
cell.setCellStyle(cellStyle);

HSSFFont headerTableFont = wb.createFont();
headerTableFont.setFontHeightInPoints((short) 10);
headerTableFont.setColor(IndexedColors.WHITE.getIndex());

HSSFCellStyle tableHeaderStyle = wb.createCellStyle();
tableHeaderStyle.setDataFormat(format.getFormat("@"));
tableHeaderStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
tableHeaderStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
tableHeaderStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
tableHeaderStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
tableHeaderStyle.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index);
tableHeaderStyle.setFont(headerTableFont);
tableHeaderStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

HSSFRow tableHeaderRowRow = spreadSheet.createRow(rowIndex++);

for (int i = 0; i < header.size(); i++)
{
HSSFCell ipHeaderCell = tableHeaderRowRow.createCell(i);
ipHeaderCell.setCellStyle(tableHeaderStyle);
ipHeaderCell.setCellValue(header.get(i));
spreadSheet.autoSizeColumn(i);
}

/*
* ********************** DATA *******************
*/
HSSFCellStyle dataCellStyle = wb.createCellStyle();
dataCellStyle.setDataFormat(format.getFormat("@"));
dataCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
dataCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
dataCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
dataCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);

//spreadSheet.createRow(rowIndex++);
for (LinkedHashMap<String, String> row : data)
{
HSSFRow currentRow = spreadSheet.createRow(rowIndex++);
for (int i = 0; i < header.size(); i++)
{
HSSFCell dataCell = currentRow.createCell(i);
dataCell.setCellStyle(dataCellStyle);
dataCell.setCellType(Cell.CELL_TYPE_STRING);

String celVal = row.get(header.get(i));
dataCell.setCellValue(celVal);
}
}

// Resize columns automatically.
for (int i = 0; i < header.size(); i++)
{
spreadSheet.setDefaultColumnStyle(i, style);
spreadSheet.autoSizeColumn(i);
}

FileOutputStream resultOutStream = null;
try
{
resultOutStream = new FileOutputStream(result);
wb.write(resultOutStream);
resultOutStream.flush();
}
catch (Exception e)
{
Logger.ERROR("File path: " + result.getAbsolutePath() + File.separator + result.getName(), e);
throw new RuntimeException("File path: " + result.getAbsolutePath() + File.separator + result.getName(), e);
}
finally
{
try
{
if (resultOutStream != null)
{
resultOutStream.close();
}
}
catch (Exception ignore)
{

}
}

return result;
}


What seems to be the problem?



asked 33 secs ago

Snow

212






java.lang.ClassNotFoundException: sun.awt.X11FontManager from intellij using jdk 1.7.0_71

Aucun commentaire:

Enregistrer un commentaire