Search code examples
javaapixssf

Java - Trouble loading file NoClassDefFoundError


Solved: I needed to add another jar file that was in another folder

All of my code is below. It is from this website. I've read over multiple examples of loading XSSF files, but I continue to get this same error. All of my imports are correct, but my only guess would be my file path. But it seems correct and gives me no errors

package testcode;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


import java.util.Iterator;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TestCode {

public static void main( String[] args ) throws IOException {
        InputStream ExcelFileToRead = new FileInputStream("C:/Users/[name]/Desktop/Book1.xlsx");
        XSSFWorkbook  wb = new XSSFWorkbook(ExcelFileToRead);

        XSSFWorkbook test = new XSSFWorkbook(); 

        XSSFSheet sheet = wb.getSheetAt(0);
        XSSFRow row; 
        XSSFCell cell;

        Iterator rows = sheet.rowIterator();

        while ( rows.hasNext() ) {
            row = ( XSSFRow ) rows.next();
            Iterator cells = row.cellIterator();
            while( cells.hasNext() ) {
                cell = ( XSSFCell ) cells.next();

                if ( cell.getCellType() == XSSFCell.CELL_TYPE_STRING ) {
                    System.out.print( cell.getStringCellValue() + " " );
                }
                else if( cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC ) {
                    System.out.print( cell.getNumericCellValue() + " " );
                }
                else {

                }
            }
            System.out.println();
        }
}

Error message:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException at testcode.TestCode.main(TestCode.java:20) Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException 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) ... 1 more


Solution

  • NoClassDefFoundError . This exception is encountered, when there is a class file or api your code depends on,is present at compile time but not found at runtime. Please check the jars available at runtime or compare the runtime and compile dependencies.