TacoTuesday
TacoTuesday

Reputation: 67

where are my mistakes?

I have a problem with reading excel file and analyze it through apache poi 3.9... I added the external JAR file but it still gives me errors. Here my codes

import java.io.File;
import java.io.FileInputStream;

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 loop {
     public static void main(String [] args) throws Exception
     {
         File excel= new File("C:\\Users\\songSent.xlsx");
         FileInputStream fis= new FileInputStream(excel);
         XSSFWorkbook wb= new XSSFWorkbook(fis);
         XSSFSheet ws= wb.getSheet("Input");

         int rowNum=ws.getLastRowNum() +1;
         int colNum=ws.getRow(0).getLastCellNum();
         String [][] data= new String[rowNum][colNum];

         for(int i=0; i<rowNum; i++)
         {
             XSSFRow row= ws.getRow(i);
             for(int j=0; j<colNum; j++)
             {
                XSSFCell cell=row.getCell(j);
                String value=cellToString(cell);

                data[i][j]=value;
                System.out.println("the value is " +value);
             }
         }
    }
    public static String cellToString(XSSFCell cell)
    {
        int type;
        Object result;
        type=cell.getCellType();

        switch (type){

        case 0:
          result=cell.getNumericCellValue();
          break;
        case 1:
          result=cell.getStringCellValue();
          break;
        default:
            throw new RuntimeException("There no support");

        }
        return result.toString();
     }
  }

and these are the errors when I run the program

Exception in thread "main" java.lang.NoClassDefFoundError:org/apache/poi/hssf/usermodel/HSSFCell
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by:     java.lang.ClassNotFoundException:org.apache.poi.hssf.usermodel.HSSFCell
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)
... 6 more

Upvotes: 0

Views: 1036

Answers (2)

Gagravarr
Gagravarr

Reputation: 48376

At runtime, you're missing at least some of the Apache POI jars. Specifically, you're missing the main poi jar (eg poi-3.10-beta2-20130904.jar), and quite possibly some others too.

Firstly, you need to remember that with Java, your jars need to be available both at compile time and at runtime. Just having the jar on your classpath when you compile isn't enough, you also still need to put them on the classpath at runtime!

Secondly, you'll need to ensure that you include the Apache POI jars as appropriate for the POI components you're using, along with any dependencies those components have. That's all documented on the Apache POI components page, which explains which Jars hold which components, and what dependencies those have.

(Since you're using XSSF, that means you need the main poi jar, the poi-ooxml jar, the poi-ooxml-schemas jar, and their dependencies)

Upvotes: 0

SarZ
SarZ

Reputation: 266

It is pretty clear from the stack trace that your program is not able to find "poi" jar. Check if your classpath is set correctly & if you are running from eclipse (or some other IDE) check if jar is added to the build path.

Upvotes: 3

Related Questions