0

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

Dominic
  • 164
  • 2
  • 11
  • please provide the entire stack trace, NoClassDefFoundError has nothing to do with file path – Abhishek Jul 01 '15 at 19:35
  • @Abhishek Sorry that is my bad. It slipped my mind! Added – Dominic Jul 01 '15 at 19:37
  • If I were to hazard a guess, I'd say the library you're using has a dependency that's missing. You can load the library's classes fine, but when you call the file load method, it searches in the classpath for a dependency and can't find one. – thomas88wp Jul 01 '15 at 19:38
  • Verify that all required Java classes are included in the application’s classpath. The most common mistake is not to include all the necessary classes, before starting to execute a Java application that has dependencies on some external libraries. – Abhishek Jul 01 '15 at 19:42

1 Answers1

1

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.

Prateek Kapoor
  • 947
  • 9
  • 18