title: NoClassDefFoundError: UnsupportedFileFormatException while working with excel sheet using java
body: <p>I'm writing a Java program which reads from a .xlsx file and give the output in .csv format. This is my code:</p>
  
  <pre><code>import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.OutputStreamWriter;
  import java.io.Writer;
  import java.text.SimpleDateFormat; 
  import java.util.Date;
  import java.util.Iterator;
  import org.apache.poi.xssf.usermodel.XSSFSheet;
  import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  import org.apache.poi.ss.usermodel.Cell;   
  import org.apache.poi.ss.usermodel.DateUtil;
  import org.apache.poi.ss.usermodel.Row;
  
  public class xlsxToCsv {
  
      public static void main(String[] args) {
  
          long startTime = System.currentTimeMillis();
  
          File inputFile = new File("C:\\inputFile.xlsx");
          File outputFile = new File("C:\\outputFile.csv");
  
          xlsx(inputFile, outputFile);
  
          long stopTime = System.currentTimeMillis();
          long elapsedTime = stopTime - startTime;
  
          System.out.println(elapsedTime);
  
     }
  
     private static void xlsx(File inputFile, File outputFile) {
  
          //for storing data into CSV files
          StringBuffer data = new StringBuffer();
  
          try {
  
              Writer w = new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8");
  
              // Get the workbook object for XLS file
              XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(inputFile));
  
              // Get first sheet from the workbook
              XSSFSheet sheet = workbook.getSheetAt(0);
              Cell cell;
              Row row;
  
              // Iterate through each rows from first sheet
              Iterator&lt;Row&gt; rowIterator = sheet.iterator();
              while (rowIterator.hasNext()) 
              {
                  row = rowIterator.next();
                  // For each row, iterate through each columns
                  Iterator&lt;Cell&gt; cellIterator = row.cellIterator();
                  while (cellIterator.hasNext()) 
                  {
                      cell = cellIterator.next();
  
                      switch (cell.getCellType()) 
                      {
                          case Cell.CELL_TYPE_BOOLEAN:
                              data.append(cell.getBooleanCellValue() + ",");
                              break;
  
                          case Cell.CELL_TYPE_NUMERIC:
                              if(DateUtil.isCellDateFormatted(cell)) {
                                  Date date = cell.getDateCellValue();
                                  System.out.println(date.toString());
                                  SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                                  String d = sdf.format(date);
                                  System.out.println(d);
                                  data.append(d + ",");   
                              }
                              else if(cell.getNumericCellValue() == (int)cell.getNumericCellValue())
                                  data.append((int)cell.getNumericCellValue() + ",");
                              else if(cell.getNumericCellValue() == (long)cell.getNumericCellValue())
                                  data.append((long)cell.getNumericCellValue() + ",");
                              else
                                  data.append(cell.getNumericCellValue() + ",");
                              break;
  
                          case Cell.CELL_TYPE_STRING:
                                  data.append((cell.getStringCellValue()) + ",");
                                  break;
  
                          case Cell.CELL_TYPE_BLANK:
                                  data.append("" + ",");
                                  break;
  
                          default:
                                  data.append(cell + ",");
                          }
  
                          //data.append('\n'); 
                  }
                  data.append("\r\n");
          }
  
          w.write(data.toString());
          w.close();
  
      } catch(FileNotFoundException e) {
          e.printStackTrace();
      } catch(IOException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>However, I'm getting the following error:</p>
  
  <pre><code>Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException
  at java.lang.ClassLoader.defineClass1(Native Method)
  at java.lang.ClassLoader.defineClass(Unknown Source)
  at java.security.SecureClassLoader.defineClass(Unknown Source)
  at java.net.URLClassLoader.defineClass(Unknown Source)
  at java.net.URLClassLoader.access$100(Unknown Source)
  at java.net.URLClassLoader$1.run(Unknown Source)
  at java.net.URLClassLoader$1.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(Unknown Source)
  at java.lang.ClassLoader.loadClass(Unknown Source)
  at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
  at java.lang.ClassLoader.loadClass(Unknown Source)
  at xlsxToCsv.xlsxToCsv.xlsx(xlsxToCsv.java:47)
  at xlsxToCsv.xlsxToCsv.main(xlsxToCsv.java:28)
  Caused by: java.lang.ClassNotFoundException: org.apache.poi.UnsupportedFileFormatException
  at java.net.URLClassLoader$1.run(Unknown Source)
  at java.net.URLClassLoader$1.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(Unknown Source)
  at java.lang.ClassLoader.loadClass(Unknown Source)
  at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
  at java.lang.ClassLoader.loadClass(Unknown Source)
  ... 14 more
  </code></pre>
  
  <p>I have included the following jars:</p>
  
  <ul>
  <li>dom4j-1.6.jar</li>
  <li>poi-3.9.jar</li>
  <li>poi-ooxml-3.11.jar</li>
  <li>poi-ooxml-schemas-3.8-20120326.jar</li>
  <li>xmlbeans-2.3.0.jar</li>
  </ul>
  
  <p>I've checked the file format is .xlsx and also the directory but I don't understand what the problem is.</p>
  
  <p>How can I remove this error?</p>