NoClassDefFoundError: UnsupportedFileFormatException when using apache poi to write to excel file
I am trying to write an excel file (.xlsx) using Apache poi, I have included apache poi dependencies in my pom.xml file. But I am running the following exception.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at test.ExcelWriting.main(ExcelWriting.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.UnsupportedFileFormatException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 13 more
The code and pom.xml are specified as follows.
I am getting an exception on the next line.
XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);
code:
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelWriting {
public static void main(String[] args) throws IOException{
File myFile = new File("/home/sabra/workspace/test/src/main/resources/test.xlsx");
FileInputStream fis = new FileInputStream(myFile);
// Finds the workbook instance for XLSX file
XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);
// Return first sheet from the XLSX workbook
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
Map<String, Object[]> data = new HashMap<String, Object[]>();
data.put("7", new Object[] {7d, "Sonya", "75K", "SALES", "Rupert"});
data.put("8", new Object[] {8d, "Kris", "85K", "SALES", "Rupert"});
data.put("9", new Object[] {9d, "Dave", "90K", "SALES", "Rupert"});
// Set to Iterate and add rows into XLS file
Set<String> newRows = data.keySet();
// get the last row number to append new data
int rownum = mySheet.getLastRowNum();
for (String key : newRows) {
// Creating a new Row in existing XLSX sheet
Row row = mySheet.createRow(rownum++);
Object [] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
Cell cell = row.createCell(cellnum++);
if (obj instanceof String) {
cell.setCellValue((String) obj);
} else if (obj instanceof Boolean) {
cell.setCellValue((Boolean) obj);
} else if (obj instanceof Date) {
cell.setCellValue((Date) obj);
} else if (obj instanceof Double) {
cell.setCellValue((Double) obj);
}
}
}
// open an OutputStream to save written data into XLSX file
FileOutputStream os = new FileOutputStream(myFile);
myWorkBook.write(os);
myWorkBook.close();
}
}
Pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11-beta2</version>
</dependency>
</dependencies>
</project>
+3
source to share
2 answers
I think you are missing some "UnsupportedFileFormatException" classes
try to change poi versions to the same and don't use 3.11-beta2
You can use both version 3.12 http://mvnrepository.com/artifact/org.apache.poi
+4
source to share
I tried my code using Jar Libs, not maven (see project images) and it worked fine. So as Ati said you are missing some libraries or your file type is not xlsx.
PS: I just changed this line of code from
File myFile = new File("/home/sabra/workspace/test/src/main/resources/test.xlsx");
to
File myFile = new File("test.xlsx");
+1
source to share